如何在 java 中重置 FileReader 而不必彻底更改我的程序?
How to reset FileReader in java without having to change my program drastically?
我发现 FileReader
扫描文件 only
一次。之后,如果你想在程序中,你必须关闭它并重新初始化它以重新扫描文件。我在其他博客和 Whosebug 问题中读到了这一点,但其中大多数提到了 BufferedReader
或其他类型的读者。问题是我已经使用 FileReader
完成了我的程序,我不想将所有内容更改为 BufferedReader
,所以无论如何要重置文件指针而不引入任何其他 类 还是方法?还是只是用 BufferedReader
包裹我已经存在的 FileReader
?这是我专门为这个问题写的一小段代码,如果我可以用 BufferedReader
包裹我的 FileReader
,我希望你用这个代码片段来做。
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class Files {
public static void main(String args[]) throws IOException{
File f = new File("input.txt");
FileReader fr = new FileReader(f);
int ch;
while((ch = fr.read()) != -1){
// I am just exhausting the file pointer to go to EOF
}
while((ch = fr.read()) != -1){
/*Since fr has been exhausted, it's unable to re-read the file now and hence
my output is empty*/
System.out.print((char) ch);
}
}
}
谢谢。
像这样使用java.io.RandomAccessFile
:
RandomAccessFile f = new RandomAccessFile("input.txt","r"); // r=read-only
int ch;
while ((ch = f.read()) != -1) {
// read once
}
f.seek(0); // seek to beginning
while ((ch = f.read()) != -1) {
// read again
}
EIDT ------------
BufferedReader
也有效:
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
br.mark(1000); // mark a position
int ch;
if ((ch = br.read()) != -1) {
// read once
}
br.reset(); // reset to the last mark
if ((ch = br.read()) != -1) {
// read again
}
但使用时请注意 mark()
:
BufferedReader
中的mark
方法:public void mark(int readAheadLimit) throws IOException
。这是从 javadoc 复制的用法:
Limit on the number of characters that may be read while still preserving the mark. An attempt to reset the stream after reading characters up to this limit or beyond may fail. A limit value larger than the size of the input buffer will cause a new buffer to be allocated whose size is no smaller than limit. Therefore large values should be used with care.
我发现 FileReader
扫描文件 only
一次。之后,如果你想在程序中,你必须关闭它并重新初始化它以重新扫描文件。我在其他博客和 Whosebug 问题中读到了这一点,但其中大多数提到了 BufferedReader
或其他类型的读者。问题是我已经使用 FileReader
完成了我的程序,我不想将所有内容更改为 BufferedReader
,所以无论如何要重置文件指针而不引入任何其他 类 还是方法?还是只是用 BufferedReader
包裹我已经存在的 FileReader
?这是我专门为这个问题写的一小段代码,如果我可以用 BufferedReader
包裹我的 FileReader
,我希望你用这个代码片段来做。
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class Files {
public static void main(String args[]) throws IOException{
File f = new File("input.txt");
FileReader fr = new FileReader(f);
int ch;
while((ch = fr.read()) != -1){
// I am just exhausting the file pointer to go to EOF
}
while((ch = fr.read()) != -1){
/*Since fr has been exhausted, it's unable to re-read the file now and hence
my output is empty*/
System.out.print((char) ch);
}
}
}
谢谢。
像这样使用java.io.RandomAccessFile
:
RandomAccessFile f = new RandomAccessFile("input.txt","r"); // r=read-only
int ch;
while ((ch = f.read()) != -1) {
// read once
}
f.seek(0); // seek to beginning
while ((ch = f.read()) != -1) {
// read again
}
EIDT ------------
BufferedReader
也有效:
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
br.mark(1000); // mark a position
int ch;
if ((ch = br.read()) != -1) {
// read once
}
br.reset(); // reset to the last mark
if ((ch = br.read()) != -1) {
// read again
}
但使用时请注意 mark()
:
BufferedReader
中的mark
方法:public void mark(int readAheadLimit) throws IOException
。这是从 javadoc 复制的用法:
Limit on the number of characters that may be read while still preserving the mark. An attempt to reset the stream after reading characters up to this limit or beyond may fail. A limit value larger than the size of the input buffer will cause a new buffer to be allocated whose size is no smaller than limit. Therefore large values should be used with care.