我正在使用 Java 从文件中读取文本。这是我的代码:
public void readCurrentPage(){
FileInputStream file = null;
BufferedInputStream buff = null;
int readByteValue;
Exception lastShownException = null;
boolean errorShown = false;
boolean readOnce;
try{
errorShown = false;
file = new FileInputStream("/Volumes/Storage Drive/eclipse/workspace/Nicholas Planner/bin/data/test.txt");
buff = new BufferedInputStream(file,8*1024);
while (true){
readByteValue = buff.read();
if (readByteValue == -1){
break;
}
System.out.print((char) readByteValue + " ");
}
}catch(Exception e){
if(errorShown == false && lastShownException!=e){
JOptionPane.showMessageDialog(null, "There was an error: \n"+e, "Error!", 1);
e = lastShownException;
errorShown = true;
}
}finally{
try{
errorShown = false;
buff.close();
file.close();
}catch(Exception e){
if(errorShown == false && lastShownException!=e){
JOptionPane.showMessageDialog(null, "There was an error: \n"+e, "Error!", 1);
e = lastShownException;
errorShown = true;
}
}
}
}
这是文件的文本:
test
This is cool!
当我使用上面的代码读取文件时,我得到的是:
t e s t
T h i s i s c o o l ! t e s t
T h i s i s c o o l !
为什么我的代码会重复文件的文本?