2015-01-09 46 views
-3

大家好,我的BufferedWriter一直有問題。起初,我只有1名緩衝作家被表現爲cc 1和這個工作完美地與輸出一起,但現在,我已經嘗試實現2我繼續得到一個錯誤別的地方Java - 兩位緩衝作家

Multiple markers at this line 
    - Syntax error, insert "}" to complete Statement 
    - Syntax error, insert "Finally" to complete 

這裏是碼。在沒有的情況下 - 錯誤在}else if (command.equals("action 2"))

if (selected) { 
    if (command.equals("action1")) { 
     BufferedWriter aa; 
     try { 
      File writerB = new File("output1.txt"); //set transaction-list.txt to be the destination file when writeTransactions is used 
      if (!writerB.exists()) { 
       writerB.createNewFile(); 
        }       

     FileWriter bb = new FileWriter(writeBalance, true); //filewriter 
     aa = new BufferedWriter(bb); // bufferedwriter 

     BufferedWriter cc; 
      try { 
       int x=0; 
       File writerT = new File("output2.txt"); //set transaction-list.txt to be the destination file when writeTransactions is used 
       if (!writerT.exists()) { 
        writerT.createNewFile(); 
        } 
       FileWriter dd = new FileWriter(writeTransactions, true); //filewriter 
       cc = new BufferedWriter(dd); // bufferedwriter 
       String newLine = System.getProperty("line.separator"); //creates a line separator which will be used with string newLine 

     if (n==0) { 
      bw.write(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(now) + newLine); 
      wb.write(c);} 
      bw.close; 
      wb.close} 

    }else if (command.equals("action 2")) 
+0

請格式化您的代碼以便於閱讀!另外,這個代碼之後是什麼? – meskobalazs

+1

這是2015年,所以請使用java.nio.file(例如:你不檢查File.createNewFile()')的結果。請參閱[這裏](http://java7fs.wikia.com/wiki/Using_the_java.nio.file_API)和[這裏](http://java7fs.wikia.com/wiki/Why_File_sucks)。 – fge

+0

我強烈建議您正確縮進代碼(大多數IDE都有一個用於此的菜單選項)。這會幫助你看到你有一個無與倫比的'{'以及幫助我​​們幫助你。 – RealSkeptic

回答

1

您無法正確處理您的資源;對於一個他們應該被關閉在finally塊...

...但是,還有更好的,那就是使用try-with-resources聲明以及新的java.nio.file API:

final Path file1 = Paths.get("output1.txt"); 
final Path file2 = Paths.get("output2.txt"); 

try (
    final BufferedWriter writer1 = Files.newBufferedWriter(file1, StandardCharsets.UTF_8, 
     StandardOpenOption.CREATE, StandardOpenOption.APPEND); 
    final BufferedWriter writer2 = Files.newBufferedWriter(file2, StandardCharsets.UTF_8, 
     StandardOpenOption.CREATE, StandardOpenOption.APPEND); 
) { 
    // use writer1 and writer2 here. 
    // Note that BufferedWriter has a .newLine() method as well. 
} 
0

你沒有做出catchtry後開始。 A catchfinally區塊是強制try之後。

如前所述,強烈建議在Java 7中引入try-with-resources語句。

+0

當我添加一個catch它使'FileWriter bb = new FileWriter(writeBalance,true); aa =新的BufferedWriter(bb);'這是在嘗試超出範圍 –

+0

當然是。在'try'塊之外聲明'FileWriter'。 – meskobalazs