8 Replies Latest reply: Oct 12, 2012 8:26 AM by gimbal2 RSS

    BufferedWriter skipping last 10 lines

    900707
      Hello Experts,
      I have developed one program which is reading line by line from a file and replacing comma with null.

      sample file1

      alex|,20|,uk|,255555|,
      ...............
      ...............

      after replace and writing to a new file

      alex|20|uk|255555|
      .............
      .............

      The problem is that though it is replacing properly but at the end i can see few lines are missing in the new file. I am not sure whats the reason. I guess it related out of memory problem but i am not getting any error. Here is my code. My files contains around 20,000 lines.

      String curdate=new SimpleDateFormat("yyyyMMdd").format(Calendar.getInstance().getTime()).toString();
      String filename= "D:/FF/1CIM_20121012.txt";
      try
      {
      FileReader fr = new FileReader(filename);
      BufferedReader br = new BufferedReader(fr);
      FileWriter fw = new FileWriter("D:/FF/123CIM_20121012.txt");
      BufferedWriter bw = new BufferedWriter(fw);
      String line = null;
      while((line=br.readLine()) != null) { 
      line= line.replaceAll(",","");
      bw.write(line);
      bw.newLine();
      }
      fr.close();br.close();fw.close();bw.close();
      }
      catch(IOException ioe)
      {
      System.err.println("IOException: " + ioe.getMessage());
      }

      Please suggest.
        • 1. Re: BufferedWriter skipping last 10 lines
          gimbal2
          I don't see much wrong; i do see you close the filewriter AND the bufferedwriter; try only closing the bufferedwriter (which will in turn close the filewriter it is wrapped around). It might just be that because you close the filewriter before you close the bufferedwriter, you are not giving it the chance to flush the last few changes.
          • 2. Re: BufferedWriter skipping last 10 lines
            EJP
            You closed the FileWriter before closing the BufferedWriter, so the BW had no way of flushing its buffered output to the FW. Don't do that. Just close the outermost stream/reader/writer. Same goes for the input.
            • 3. Re: BufferedWriter skipping last 10 lines
              Tolls
              gimbal2 wrote:
              I don't see much wrong; i do see you close the filewriter AND the bufferedwriter; try only closing the bufferedwriter (which will in turn close the filewriter it is wrapped around). It might just be that because you close the filewriter before you close the bufferedwriter, you are not giving it the chance to flush the last few changes.
              Exactly what you suspect.
              Indeed the following code shows just that (and throws an exception, so the BW does not get closed):
                    FileWriter fw = null;
                    BufferedWriter bw = null;
                    try {
                       fw = new FileWriter("textout.txt");
                       bw = new BufferedWriter(fw);
                       bw.write("Something");
                       fw.close();
                       bw.close();
                    } catch (IOException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                    }
              ETA: Not quite right, it's the BW that throws the exception.

              Edited by: Tolls on 12-Oct-2012 05:35
              • 4. Re: BufferedWriter skipping last 10 lines
                tmazight
                The suggest of "EJP" seems to be the logical one. A good practice, try to close your streams in a finally block, because if you got an exception, your streams will stay opened.
                • 5. Re: BufferedWriter skipping last 10 lines
                  900707
                  Thank you guys. There was the mistake. Though I am not an expert in java but i thought i am closing them after the while loop. So after writing everything to the file it will go to the next statement that is bw.close();fw.close(); br.close();fr.close();

                  But my guess was wrong.
                  Thank You All.
                  • 6. Re: BufferedWriter skipping last 10 lines
                    939520
                    On a side note, you might consider replacing this:
                    System.err.println("IOException: " + ioe.getMessage());
                    with this
                    e.printStackTrace();
                    as was demonstrated in the previous post.

                    It will provide you with better information on tracking down bugs.
                    • 7. Re: BufferedWriter skipping last 10 lines
                      EJP
                      bw.close();fw.close(); br.close();fr.close();
                      I don't understand all of your post, but you didn't write that code in the first place, and you shouldn't write it now. Just close bw and br, as I said above.
                      • 8. Re: BufferedWriter skipping last 10 lines
                        gimbal2
                        >
                        But my guess was wrong.
                        Thank You All.
                        Guessing is always wrong ;)

                        I would read a little in the javadocs about how the buffered streams work. In fact when you run into a problem your first stop should be the javadocs of the classes you are using. You don't have to be an expert to be able to investigate and fix stuff.