簡體   English   中英

關閉 BufferedWriter 是否會擦除寫入文件的所有文件數據?

[英]Closing BufferedWriter is erasing all file data written on file?

我從ObjectInputStream獲取連續數據,我正在使用BufferedWriter對象在我的文件上寫入這些數據。 我正在檢查不斷增加的文件大小。 現在,一旦我停止使用false停止while(true)方法,該方法繼續運行此方法,我將關閉我的BufferedWriter對象。 此操作后,寫入文件的任何數據都將丟失。

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);
    }

記錄的數據:

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

您需要關閉輸出流。 當前,您正在catch子句中執行此操作,如果您沒有遇到IOException (並且您不希望發生這種情況),則不會達到該子句

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

像這樣修改你的代碼

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();
  }

好的,這里我使用了 BufferedWriter 的 FileWriter 對象 intsead,現在我的文件數據沒有被刪除。 所有的代碼都是一樣的,只是改變了 Object 對我有用。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM