2013-01-11 37 views
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」 個)

prin但它應該加載保存在「Q'sSBackupBackup.cfg」中的信息。

有人請告訴我如何解決這個問題嗎?

+0

我不知道你的代碼是什麼,但你可以使用這個鏈接正確地讀取文件。 [如何正確讀取文件](http://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml) – Smit

+0

爲什麼你的目標字符串中有空格/換行符?你檢查了你正在寫的文件嗎? – Zhedar

回答

1

的問題是,你跳過了數據的唯一行:

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

,那麼你將到達EOFStringdest將等於null下一個讀:

dest = saveFile.readLine(); 

通過而不是跳過第一行,您將從文件中讀回String