我正在尝试覆盖文本文件中的内容(作为编码程序的一部分),每当我运行代码时,文件都会变空,但是当我重新启动计算机时,内容会回到那里。
为什么会发生这种情况,我该如何解决这个问题?
fileScan = new Scanner(f);
fileWriter = new FileWriter(f);
while (fileScan.hasNext()) {
//store current line
curLine = fileScan.nextLine();
curLineParsed = new char[curLine.length()];
//parse into characters
for (int i = 0; i < curLine.length(); i++)
curLineParsed[i] = curLine.charAt(i);
//reset new line
newLine = "";
//ENCODING ONE LINE
//repeat for every code character
for (int i = 0; i < code[0].length; i++) {
//go through each character in the line
for (int j = 0; j < curLineParsed.length; j++) {
//if the character in process equals to the current character in line
if (code[0][i] == curLineParsed[j]) {
//replace character
curLineParsed[j] = code[1][i];
}
}
}
//store it in a new string
for (int i = 0; i < curLineParsed.length; i++)
newLine += curLineParsed[i];
System.out.println(newLine);
//REPLACING ENCODED LINE WITH ORIGINAL
fileWriter.write(newLine+"\n");
}
fileWriter.close();