2016-07-07 64 views
-2

我似乎無法弄清楚這一點。在下面的方法中,我試圖在2個地方寫一個布爾文件到文件中,但是實際上沒有寫入任何內容。任何幫助將不勝感激。文本沒有寫入文件

private void renameTables(){ 
    String path = MessengerMain.getInstance().getDataFolder() + File.separator + "v3-0-0 Table Rename.txt"; 
    File f = new File(path); 
    try(ResultSet rs = conn.getMetaData().getTables(null, null, "%", null); Writer w = new PrintWriter(new FileOutputStream(f, false))){ 
     if (!f.exists()){ 
      f.createNewFile(); 
      w.write("false"); 
      w.flush(); 
     } 

     List<String> lines = Files.readAllLines(Paths.get(path)); 
     if (lines.get(0).equalsIgnoreCase("false")){ 
      System.out.println("[Messenger] Verifying table names..."); 
      int count = 0; 
      List<String> tables = new ArrayList<String>(); 
      tables.add("messages"); 
      tables.add("scores"); 
      tables.add("contacts"); 
      while (rs.next()){ 
       String table = rs.getString("TABLE_NAME"); 
       if (tables.contains(table)){ 
        update("ALTER TABLE " + table + " RENAME TO " + ("messenger_" + table) + ";"); 
        count++; 
       } 
      } 
      if (count > 0){ 
       System.out.println("[Messenger] Done. " + count + " table" + (count == 1 ? "" : "s") + " renamed."); 
      }else{ 
       System.out.println("[Messenger] Done. No tables need to be renamed."); 
      } 
      w.write("true"); 
      w.flush(); 
     } 
    } catch (SQLException | IOException e){ 
     e.printStackTrace(); 
    } 
} 

繼艾略特·弗裏施的意見(相同的結果):

private void renameTables(){ 
    String path = MessengerMain.getInstance().getDataFolder() + File.separator + "v3-0-0 Table Rename.txt"; 
    File f = new File(path); 
    try(ResultSet rs = conn.getMetaData().getTables(null, null, "%", null)){ 
     Writer w = new PrintWriter(new FileOutputStream(f, false)); 
     if (!f.exists()){ 
      f.createNewFile(); 
      w.write("false"); 
      w.close(); //close here 
     } 

     List<String> lines = Files.readAllLines(Paths.get(path)); 
     if (lines.get(0).equalsIgnoreCase("false")){ 
      System.out.println("[Messenger] Verifying table names..."); 
      int count = 0; 
      List<String> tables = new ArrayList<String>(); 
      tables.add("messages"); 
      tables.add("scores"); 
      tables.add("contacts"); 
      while (rs.next()){ 
       String table = rs.getString("TABLE_NAME"); 
       if (tables.contains(table)){ 
        update("ALTER TABLE " + table + " RENAME TO " + ("messenger_" + table) + ";"); 
        count++; 
       } 
      } 
      if (count > 0){ 
       System.out.println("[Messenger] Done. " + count + " table" + (count == 1 ? "" : "s") + " renamed."); 
      }else{ 
       System.out.println("[Messenger] Done. No tables need to be renamed."); 
      } 
      w = new PrintWriter(new FileOutputStream(f, false)); //create a new writer 
      w.write("true"); 
      w.close(); //close here 
     } 
    } catch (SQLException | IOException e){ 
     e.printStackTrace(); 
    } 
} 
+2

有你嘗試通過調試進行嘗試呃?你確定這些陳述正在達成? –

+1

考慮:您正在要求我們檢查大量的代碼,以便通過檢查對其進行調試,而無需提供我們可以編譯和運行的內容。另一方面,您可以運行您的程序並在調試器中逐步完成,並可能自行找到問題。 –

+0

是的文件正在創建,我得到一個錯誤,我試圖從文件中讀取,因爲沒有任何內容 –

回答

1

這裏是一個全職工作minimal, complete, verifiable example

public static void main(String[] args) { 
    File f = new File(System.getProperty("user.home"), "temp.txt"); 
    String path = f.getPath(); 
    try (Writer w = new FileWriter(f)) { 
     w.write("false"); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    try { 
     List<String> lines = Files.readAllLines(Paths.get(path)); 
     System.out.println(lines); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

輸出是(預期)

[false] 
+0

不是我在做什麼? –

+0

減去我們無法驗證的所有內容;我只能得出結論,在你的文章中沒有達到陳述。 –