0
我想讀取文件並創建文件的副本,但我的代碼只寫入文件的最後一行。我該怎麼做,以便每當我打電話給write()
時,它都會寫入一個新行。我想爲副本創建一個新文件,所以我無法將FileWriter
構造函數添加爲true。BufferedWriter覆蓋本身
這是我的代碼:
//Create file reader
BufferedReader iReader = new BufferedReader(new FileReader(args[1]));
//Create file writer
BufferedWriter oWriter = new BufferedWriter(new FileWriter(args[2], true));
String strLine;
//reading file
int iterate = 0;
while((strLine = iReader.readLine()) != null) {
instructions[iterate] = strLine;
}
//creating duplicate
for(int i = 0; i < instructions.length; i++) {
if(instructions[i] != null) {
oWriter.write(instructions[i]);
oWriter.newLine();
} else {
break;
}
}
try {
iReader.close();
oWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
你在哪裏增加 「迭代」?它應該是指令[iterate ++] = strLine; – Ayman
啊,那是問題所在。謝謝。 – Danson