2013-01-11 3 views
0

파일을 백업 할 위치를 선택할 수있는 백업 프로그램을 만들려고합니다. 파일 위치를 저장하는 데는 문제가 없지만 읽는 것이 문제입니다.파일을 Null로 저장합니다.

저장 방법

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 ("n \"이명 령 +)

입니다

prin ts out "null"이지만 "Q'sSavesBackup.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(); 

그래서 당신은 EOF 및 도달 할를 다음 읽기에 null 동일합니다 Stringdest :

dest = saveFile.readLine(); 

이 아니고 첫 줄을 건너 뛰면 파일에서 String을 다시 읽습니다.

관련 문제