【问题标题】:Closing BufferedWriter is erasing all file data written on file?关闭 BufferedWriter 是否正在擦除写入文件的所有文件数据?
【发布时间】:2016-03-23 12:06:44
【问题描述】:

我从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

【问题讨论】:

  • 如果你不发humanReadableByteCount代码很难知道...
  • 它只是一个返回文件大小的代码。立即检查更新的问题
  • 代码看起来也很可疑。将Socket 作为这样的参数传递......这不好。
  • 我知道,但这不会造成问题。我刚刚使用了 FileWriter 并且它工作正常。

标签: java bufferedwriter objectinputstream


【解决方案1】:

您需要关闭输出流。目前,您在 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();
  }

【讨论】:

  • 是的,但它会和在我写一个像 fileOut.write(data); 这样的数据之后放它一样好文件输出.flush();文件输出.close();一旦循环继续,这将导致我出现异常。我刚刚使用 FileWriter 解决了它,但我仍然很想了解它。
  • fileOut.close()放在循环之后,当你不再写或者因为你使用追加你可以在每次循环迭代中打开和关闭,但效率较低
  • 我做到了,这给了我 java.io.IOException: Stream closed
  • 在您发布的代码中没有循环并且不可能指出您的确切错误,但是您在调用close() 之后正在写入输出流java.io。 IOException:流已关闭
【解决方案2】:

好的,我在这里使用了 BufferedWriter 的 FileWriter 对象,现在我的文件数据没有被删除。所有代码都是相同的,只是改变了 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多