I’m trying to make a backup program that allows you to choose where you want to backup your files. I’m fine with saving the file location, but reading it is the problem.
Saving Method
public static void write(String importdest) throws IOException {
// Create some data to write.
String dest = importdest;
// Set up the FileWriter with our file name.
FileWriter saveFile = new FileWriter("Q'sSavesConfig.cfg");
// Write the data to the file.
saveFile.write(dest);
// All done, close the FileWriter.
saveFile.close();
}
Variable Loading Method
public static String read() throws IOException {
String dest;
BufferedReader saveFile = new BufferedReader(new FileReader("Q'sSavesConfig.cfg"));
// Throw away the blank line at the top.
saveFile.readLine();
// Get the integer value from the String.
dest = saveFile.readLine();
// Not needed, but read blank line at the bottom.
saveFile.readLine();
saveFile.close();
// Print out the values.
System.out.println(dest + "\n");
System.out.println();
return dest;
}
My main problem is that
System.out.println(dest + “\n”)
prints out “null”, but it’s supposed to load the information held in “Q’sSavesBackup.cfg”
Could someone please show me how I can fix this?
The problem is that you are skipping the only line that has data:
so then you will reach the
EOFand theStringdestwill equalnullon the next read:By not skipping the first line, you will read back the
Stringfrom file.