我正在从ObjectInputStream获取连续数据,该数据是使用BufferedWriter对象写入文件中的。
我正在检查文件大小,并不断增加。现在,一旦我停止使用while(true)中断false方法,并继续运行该方法,就关闭了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();
  }

10-07 12:57
查看更多