⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reuseobjectoutputstream.java

📁 JAVA 2入门经典 练习答案
💻 JAVA
字号:
// Derived stream class to allow an existing object file to be used
/*
  The ObjectOutputStream constructor write a header to the file, regardless of
  whether the file exists or not. This results in an extra header being written at
  the end of an existing file that is opened in append mode. This results in a 
  StreamCorruptedException when reading of any objects added to the file.
  
  One way round this is to use ObjectOutputStream to write a new file, and this derived
  class for an existing file. You can check whether a file exists or not using the 
  exists() method for the File object. You can also check that the file contains data
  by calling the length() method for the File object.
*/
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.IOException;

public class ReuseObjectOutputStream extends ObjectOutputStream {

    public ReuseObjectOutputStream(OutputStream out) throws IOException {
      super(out);
    }

    protected void writeStreamHeader() throws IOException {
      reset();
    }
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -