Pawankumar Dubey
Pawankumar Dubey

Reputation: 387

Closing BufferedWriter is erasing all file data written on file?

I am getting continuous data from ObjectInputStream which I am writing on my file using BufferedWriter object. I am checking file size which keeps on increasing. Now as soon as I stop discontinue the while(true) method with false which keeps on running this method I am closing my BufferedWriter object. After this operation whatever data were written on file are lost.

private void appendFile(JLabel jLab0x28, Socket client)
    {   
        try
        {   
            if(!continueMe)
            {   
                fileOut.close();
                fileOut = null;
                in.close();
                System.gc();
                jLab0x28.setText("<html> <font color='red'> Socket is closed  "+client.isClosed()+" </font> </html>");
                System.out.println("File size after discontinuing  "+
                        humanReadableByteCount(new File("C:\\ISSUE124_Resolved.txt").length(), true) );
            }

            if(!client.isClosed())
            {   
                try
                {   
                    String data = in.readObject().toString();
                    System.out.println("File size is  "+
                            humanReadableByteCount(new File("C:\\ISSUE124_Resolved.txt").length(), true) );
                    fileOut.append(data);
                    fileOut.flush();
                }
                catch (EOFException exp)
                {   
                    continueMe = true;
                    exp.printStackTrace();
                    System.out.println("A Stream has finished "+exp.toString()+"\n");
                }
                catch (ClassNotFoundException exp) 
                {
                    exp.printStackTrace();
                    System.err.println(exp.toString());
                    continueMe = false;
                }
            }
        }
        catch(IOException exp)
        {
            try 
            {
                fileOut.close();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
            exp.printStackTrace();
            System.err.println("Exception "+exp.toString());
            jLab0x28.setText(exp.getMessage());
            continueMe = false;
        }
    }
public static String humanReadableByteCount(long bytes, boolean si) {
        int unit = si ? 1000 : 1024;
        if (bytes < unit) return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
        return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
    }

Logged Data:

File size is  118.3 kB
File size is  118.4 kB
File size is  118.5 kB
File size is  118.7 kB
File size is  118.8 kB
File size is  118.9 kB
File size is  119.1 kB
File size is  119.2 kB
File size is  119.3 kB
Done
Closed Socket connection from client
File size after discontinuing  0 B

Upvotes: 0

Views: 354

Answers (2)

Pawankumar Dubey
Pawankumar Dubey

Reputation: 387

Okay, here i have used FileWriter object intsead of BufferedWriter and now my file data is not getting erased. All code are same it's just change of Object that did the trick for me.

public static String humanReadableByteCount(long bytes, boolean si) {
        int unit = si ? 1000 : 1024;
        if (bytes < unit) return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
        return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
    }

    private void appendFile(JLabel jLab0x28, Socket client)
    {   
        try
        {   
            if(!continueMe)
            {   
                //fileOut.close();
                //fileOut = null;
                fw.close();
                fw = null;
                in.close();
                System.gc();
                jLab0x28.setText("<html> <font color='red'> Socket is closed  "+client.isClosed()+" </font> </html>");
                System.out.println("File size after discontinuing  "+
                        humanReadableByteCount(new File("C:\\ISSUE124_Resolved.txt").length(), true) );
            }

            if(!client.isClosed())
            {   
                try
                {   
                    String data = in.readObject().toString();
                    System.out.println("File size is  "+
                            humanReadableByteCount(new File("C:\\ISSUE124_Resolved.txt").length(), true) );
                    fw.write(data);
                    fw.flush();
                    //fileOut.write(data);
                    //fileOut.flush();

                }
                catch (EOFException exp)
                {   
                    continueMe = true;
                    exp.printStackTrace();
                    System.out.println("A Stream has finished "+exp.toString()+"\n");
                }
                catch (ClassNotFoundException exp) 
                {
                    exp.printStackTrace();
                    System.err.println(exp.toString());
                    continueMe = false;
                }
            }
        }
        catch(IOException exp)
        {
            try 
            {
                //fileOut.close();
                fw.close();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
            exp.printStackTrace();
            System.err.println("Exception "+exp.toString());
            jLab0x28.setText(exp.getMessage());
            continueMe = false;
        }
    }


File size is  1.8 MB
File size is  1.8 MB
File size is  1.8 MB
Done
Closed Socket connection from client
File size after discontinuing  1.8 MB

Upvotes: 0

Radu Ionescu
Radu Ionescu

Reputation: 3532

You need to close the outputstream. Currently you are doing that in the catch clause which is not reached if you encounter no IOException (and you do not expect this)

catch(IOException exp)
        {
            try 
            {
                fileOut.close();
            } 
  ...

Modify your code like this

catch(IOException exp)
        {

            exp.printStackTrace();
            System.err.println("Exception "+exp.toString());
            jLab0x28.setText(exp.getMessage());
            continueMe = false;
        }
try 
{
     fileOut.close();
 } 
 catch (IOException e) 
 {
      e.printStackTrace();
  }

Upvotes: 1

Related Questions