如下代碼:
package study;
import java.io.*;
public class FileReaderDemo1
{
public static void main(String[] arg) throws IOException
{
FileReader r=null;
r=new FileReader("C:\\Demo.txt");
char[] a=new char[5];
int b =0;
for(;b!=-1;)
{
b=r.read(a);
System.out.print(b+"----");
System.out.println(a);
}
r.close();
}
}
其中c:\Demo.txt內容爲hello world~
那麼打印輸出什麼?
-------------------------------------------------------------------------我是萌萌的分割線-------------------------------------------------------
鏘鏘,是
5----hello
5---- worl
2----d~orl
-1----d~orl
啊咧~~~
結論:所以說定義的char[] a只是一個數據緩衝區,reader()所返回的int型數據只是下次所讀取到的字節數。並且最終是-1結果。