Search code examples
javafilefilewriter

FileWriter not writing?


I have this snippet of code:

        try {
            f1 = new File("sink.txt");
            f1.createNewFile();
            fw = new FileWriter(f1);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ... code ...
        System.out.println(sequence);
        System.out.println(mySink.indexOf(sequence));
        String result = "";
        int firstIndex = mySink.indexOf(sequence);
        if (firstIndex >= 0) {
            System.out.println(true);
            int secondIndex = mySink.indexOf(sequence, firstIndex + sequence.length());
            if (secondIndex >= 0) {
                System.out.println(true);
                result = mySink.substring(firstIndex, secondIndex + sequence.length());
                System.out.println(result);
            }
        }
        try { // Write it to file
            fw.write(result);
        } catch (IOException e) {
            e.printStackTrace();
        } 
        System.out.println("done");

When I ranit, it printed the string sequence and the index of it inside mySink. Then it goes inside the if statements and printed out the two true and printed out result so I know result was initialized successfully. But when I look into the file sink.txt, I see that it's blank. Why is it behaving this way? Did I miss something in my code? It was working before and I added some more code and it does this. I never touch the FileWriter or the File during the execution of the program. Thanks in advance!

This is my output if you want to see:

[93, 73, 74, 81, 89, 70, 50, 80, 51, 83, 62, 13, 50, 0, 40, 98, 48, 43, 47, 89]
2000466
true
true
[93, 73, 74, 81, 89, 70, 50, 80, 51, 83, 62, 13, 50, 0, 40, 98, 48, 43, 47, 89]
[93, 73, 74, 81, 89, 70, 50, 80, 51, 83, 62, 13, 50, 0, 40, 59, 48, 43, 47, 89]
[93, 73, 74, 81, 89, 70, 50, 80, 51, 83, 62, 13, 50, 0, 81, 59, 48, 43, 47, 89]
[93, 73, 74, 81, 89, 70, 50, 80, 51, 83, 62, 13, 50, 0, 81, 98, 48, 43, 47, 89]
[93, 73, 74, 81, 89, 70, 50, 80, 51, 83, 62, 13, 50, 0, 40, 98, 48, 43, 47, 89]
done

Solution

  • The short answer is that you are not closing (or flushing) your FileWriter. That means that you application will exit with unwritten data still sitting in the file buffers.

    There are a number of other mistakes in your code. Starting from the top:

        try {
            f1 = new File("sink.txt");
            f1.createNewFile();
            fw = new FileWriter(f1);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    1. The createNewFile call is redundant. The following new FileWriter will create the file.

    2. You are catching exceptions and continuing as if nothing has happened. You >>cannot<< continue from those exceptions. The rest of your code can only work properly if you succeeded in opening the file.

    3. You don't need to catch FileNotFoundException unless you intend to handle it differently. Catching IOException is sufficient, because it is a superclass of the former.

    4. At this point, you should be using try-with-resources:

      f1 = new File("sink.txt");
      try (FileWriter fw = new FileWriter(f1)) {
      
         // compute stuff
      
         // write stuff to file
      
      } catch (FileNotFoundException ex) {
          System.out.println(ex.getMessage());
      } catch (IOException ex) {
          // This is ugly for a real app.  However, an IOException that
          // is not a FileNotFoundException is "unexpected" at this point
          // and providing a user-friendly explanation would be tricky. 
          ex.printStackTrace();  
      }
      

      The try-with-resources will cause fw to be closed automatically when the block exits. Closing the writer will first flush it.