I'm trying to overwrite the contents in a text file (as a part of an encoding program), and whenever I run the code the file becomes empty, but the contents are back there when I restart my computer.
Why is this happening, and how can I fix this?
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();