0

我正在尝试制作一个备份程序,允许您选择要备份文件的位置。我可以保存文件位置,但读取它是问题所在。

保存方法

 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();
  }

可变加载方法

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;
  }

我的主要问题是

System.out.println(dest + "\n")

打印出“null”,但它应该加载“Q'sSavesBackup.cfg”中保存的信息

有人可以告诉我如何解决这个问题吗?

4

1 回答 1

1

问题是您正在跳过唯一有数据的行:

// Throw away the blank line at the top.
saveFile.readLine(); 

因此,您将在下一次阅读时达到EOFString dest将相等:null

dest = saveFile.readLine();

通过跳过第一行,您将读回String源文件。

于 2013-01-11T00:51:52.907 回答