ファイルの行数、文字数、単語数をカウントする次のコードを作成しました。BufferedReader を使用しました。
import java.io.*;
class FileCount
{
public static void main(String args[]) throws Exception
{
FileInputStream file=new FileInputStream("sample.txt");
BufferedReader br=new BufferedReader(new InputStreamReader(file));
int i;
int countw=0,countl=0,countc=0;
do
{
i=file.read();
if((char)i==("\t"))
countw++;
else if((char)i=="\n")
countl++;
else
countc++;
}while(i!=-1);
System.out.println("Number of words"+countw);
System.out.println("Number of lines"+countw);
System.out.println("Number of characters"+countc);
}
}
問題は、バッファリングされたリーダーのみを使用する必要があることです。コードで行った char と String を比較できないことはわかっています。このコードに他の方法はありますか?