【发布时间】:2018-04-02 10:24:06
【问题描述】:
public class ReadFileUsingFileReader {
public static void main(String[] args) {
String path = "D:\\sticky_notes.txt";
readFileUsingFileReader(path);
}
public static void readFileUsingFileReader(String file) {
try {
//read the file
FileReader reader = new FileReader(file);
char[] buffer = new char[1024];
int noOfCharsRead = reader.read(buffer);
while (noOfCharsRead != -1) {
System.out.println(String.valueOf(buffer, 0, noOfCharsRead));
noOfCharsRead = reader.read(buffer);
}
reader.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
这里的变量“reader”从文件中读取前 1024 个字符并将其保存到缓冲区中。 现在再次在 while 循环中,它读取接下来的 1024 个字符。 我的问题是第二次阅读器如何知道它应该从哪个索引开始阅读下一个字符。是否有某种标志,如果有,如何访问它。
【问题讨论】:
-
它没有。操作系统提供对文件的顺序访问。
FileReader不需要对此做任何事情。
标签: java file-io buffer bufferedreader fileinputstream