-3

Hello all ive been having issues with my BufferedWriter. at first i only had 1 buffered writer the 1 that is showed as cc and this worked perfectly along with the output but now that i have tried to implement 2 i continue to get an error else where

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

Here is the code. The error starts at the }else if (command.equals("action 2")) line

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")) 
  • Please format your code to be easier to read! Also, what is after this code? – meskobalazs Jan 09 '15 at 13:07
  • 1
    This is 2015 so please use java.nio.file (just for example: you don't check for the result of `File.createNewFile()`). See [here](http://java7fs.wikia.com/wiki/Using_the_java.nio.file_API) and [here](http://java7fs.wikia.com/wiki/Why_File_sucks). – fge Jan 09 '15 at 13:09
  • I would strongly advise you to indent your code properly (most IDEs have a menu option for this). This would help you to see where you have an unmatched `{` as well as help us help you. – RealSkeptic Jan 09 '15 at 13:18

2 Answers2

1

You don't handle your resources properly; for one they should be closed in finally blocks...

... However, there is even better and that is to use a try-with-resources statement, along with the new 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.
}
fge
  • 119,121
  • 33
  • 254
  • 329
0

You did not make a catch block after the try - in neither cases. A catch or a finally block is mandatory after try.

As it was mentioned, the try-with-resources statement introduced in Java 7, is strongly recommended.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63