2016-01-29 43 views
0

當我運行這個代碼時,它可以工作,但它將文件保存在''/ Users/alencerimpasic/Dokument/java2015/projekt /''而不是' 「/用戶/ alencerimpasic /庫門/ java2015/PROJEKT/Glosforhor'我的方法編譯但沒有做我想做的事。我希望它保存文件到一個特定的地圖,但它不會這樣做

public static void SparaTillFil(List<Glosförhör> data, String filnamn){ 
    try { 
     String filename = "/Users/alencerimpasic/Dokument/java2015/projekt/Glosforhor/" + filnamn; 
     Glosförhör tempGlosförhör = new Glosförhör(); 
     PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter(filnamn))); 
     int n; 
     for (n = 0; n < data.size(); n++) {   
      tempGlosförhör = data.get(n); 
      outFile.println(tempGlosförhör.getglosa() + "\n" + 
          tempGlosförhör.getöversättning() + "\n"); 
     } 
     outFile.close(); 
    } 
    catch(IOException e) { 
     JOptionPane.showMessageDialog(null,"It failed"); 
    } 
}//SparaTillFil ends 

public static void GöraGlosförhör(List<Glosförhör> data, List<String> sl){ 
    String språk = JOptionPane.showInputDialog(null, "Write the language the test will be on."); 
    String temp = JOptionPane.showInputDialog(null, "Write the ammount of words you're going to use in the test."); 
    int antal = Integer.parseInt(temp); 
    for(int n = 0; n < antal; n++) { 
     String glosa = JOptionPane.showInputDialog(null, "Write the word!"); 
     String översättning = JOptionPane.showInputDialog(null, "Write the translation!"); 
     data.add(new Glosförhör(glosa, översättning)); 
    } 
    sl.add(språk); 
    SparaTillFil(data, språk + ".txt"); 


}//GöraGlosförhör ends 
+0

更換

PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter(filnamn))); 

我改成了「」新FILEWRITE(文件名)「」,但現在它說'它沒有「」這意味着它無法保存到文件。我不知道是什麼問題。 – Jookah

+0

噢,我的...你能否讓你的代碼[SSCCE](http://sscce.org/)? – SeniorJD

回答

1

雖然你創建一個字符串filename,你不使用它。您正在使用new FileWriter(filnamn)而不是new FileWriter(filename)。我假設

「/用戶/ alencerimpasic /庫門/ java2015/PROJEKT /」

是項目的根,因此FileWriter只需選擇作爲該目錄保存到。

0

如果您有:

PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter(filnamn))) 

我相信你想:

PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter(filename))); 

這就是爲什麼非常相似命名變量可以是源PF錯誤的例子。

0

嘗試

PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter(filnamne))); 
相關問題