When I used the method of workWithFileReader(),try to get the how many char in this document,and get the result : 547. And When I used the method of workWithBufferedReader(),try to get the how many char in this document,and get the result : 526.
I'm not sure which correct,so I used the word to get the char(include space),and get the answer :526.
Please help to example the different between them.
public static void workWithFileReader() {
int i;
long len; //
int countChar = 0; //read byte +1
int countLine = 0; //get char(10) +1
File file = new File("/Users/wayne/Downloads/Sample.txt");
len=file.length();
try {
FileReader fr = new FileReader(file);
//len = fr.available();
while((i=fr.read()) != -1) {
System.out.print((char)i);
countChar++;
if((char) i == 10) {
countLine++;
}
}
System.out.println("----------------------");
System.out.println("共有"+len+"個位元組");
System.out.println("共有"+countChar+"個字元");
System.out.println("共有"+countLine+"列資料");
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void workWithBufferedReader() {
String str;
long len;
int countChar = 0;
int countLine = 0;
File file = new File("/Users/wayne/Downloads/Sample.txt");
len = file.length();
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while((str=br.readLine()) != null) {
System.out.println(str);
countChar += str.length();
countLine++;
}
System.out.println("----------------------");
System.out.println("共有"+len+"個位元組");
System.out.println("共有"+countChar+"個字元");
System.out.println("共有"+countLine+"列資料");
br.close();
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}