【问题标题】:Reads Save Files As null读取将文件另存为空
【发布时间】:2013-01-11 00:46:23
【问题描述】:

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

保存方法

 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”中的信息

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

【问题讨论】:

  • 我不知道您的代码是什么,但是您可以使用此链接正确读取文件。 How to read file properly
  • 为什么你的目标字符串中有空格/换行符?你检查你正在写的文件了吗?

标签: java null load save loading


【解决方案1】:

问题是你跳过了唯一有数据的行:

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

那么你将到达EOFString dest 将等于null 在下一次阅读:

dest = saveFile.readLine();

通过跳过第一行,您将从文件中读回String

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 2020-02-28
    • 2015-04-03
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多