JAVA高手来看看这个问题。。。急。。。

来源:百度知道 编辑:UC知道 时间:2024/06/07 15:14:18
java 中。使用对象流写对象。都会默认的在对象前面加什么AC啊FC啊的。所谓的“魔力数字”,在进行文件追加时。。。也就是我首先用ObjectOutputStream写了一个对象到了文件中。。然后我运行程序又用ObjectOutputStream写一个对象进去,然后将对象从文件里读出来就会抛异常。。。我们老师说要将ObjectOutputStream方法重写。但我不知道怎么用。。代码如下:
class CustomFileOutputStream extends ObjectOutputStream
{
static File file;

public static CustomFileOutputStream newInstance(File _file, OutputStream out) throws IOException
{
file = _file;
return new CustomFileOutputStream(out, file);
}

private CustomFileOutputStream(OutputStream out, File file) throws IOException
{
super(out);
}

protected void writeStreamHeader() throws IOException
{
if (!file.exists() || (file.exists() && file.length() == 0))
{
super.writeStreamHeader();
}
}
}

哥们儿,你为什么不问问你的老师。他可比我们要内行了不懂就要问。

class CustomFileOutputStream extends ObjectOutputStream
{
static File file;

public static CustomFileOutputStream newInstance(File _file, OutputStream out) throws IOException
{
file = _file;
return new CustomFileOutputStream(out, file);
}

private CustomFileOutputStream(OutputStream out, File file) throws IOException
{
super(out);
}

protected void writeStreamHeader() throws IOException
{
if (!file.exists() || (file.exists() && file.length() == 0))
{
super.writeStreamHeader();
}
}
}

什么嘛 我怎么觉得像藏宝图