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 !
コードがファイルのテキストを複製するのはなぜですか?