public class test0 {
public static void main(String[] args)throws Exception {
Objectout o = new Objectout(new File(new File("test.dat")));
o.writeObject(new A());
o.close();
}
}
class A{
}
应该是因为Objectout 这个类是接口,需要实现的他的接口方法才能使用,需要使用的方法就在实现类中实现一下,不要使用的方法就可以暂时不实现,可以暂时只留一个空方法在那里,程序一般会在项目根目录下生成一个名为object.txt的文件,里面有写入的类的信息,下面是一个简单的实现,供参考:
参考链接:
java写入文件的几种方法(一)_m0_67401746的博客-CSDN博客_java 写文件
java.io 接口 ObjectOutput - Java 中文参考手册 - 文江博客
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutput;
public class test implements ObjectOutput {
public static void main(String[] args)throws Exception {
test t = new test(); //test类实现了ObjectOutput 接口的方法,然后就可以调用接口定义的方法了。
t.writeObject(new A());
}
@Override
public void writeBoolean(boolean v) throws IOException {
// TODO Auto-generated method stub
}
@Override
public void writeByte(int v) throws IOException {
// TODO Auto-generated method stub
}
@Override
public void writeShort(int v) throws IOException {
// TODO Auto-generated method stub
}
@Override
public void writeChar(int v) throws IOException {
// TODO Auto-generated method stub
}
@Override
public void writeInt(int v) throws IOException {
// TODO Auto-generated method stub
}
@Override
public void writeLong(long v) throws IOException {
// TODO Auto-generated method stub
}
@Override
public void writeFloat(float v) throws IOException {
// TODO Auto-generated method stub
}
@Override
public void writeDouble(double v) throws IOException {
// TODO Auto-generated method stub
}
@Override
public void writeBytes(String s) throws IOException {
// TODO Auto-generated method stub
}
@Override
public void writeChars(String s) throws IOException {
// TODO Auto-generated method stub
}
@Override
public void writeUTF(String s) throws IOException {
// TODO Auto-generated method stub
}
@Override
//https://blog.csdn.net/m0_67401746/article/details/123638786
//实现接口的此方法,然后就可以在实现类中调用它
public void writeObject(Object obj) throws IOException {
// TODO Auto-generated method stub
File file =new File("object.txt");
if(!file.exists()){
file.createNewFile();
}
//使用true,即进行append file
FileWriter fileWritter = new FileWriter(file.getName(),true);
fileWritter.write(obj.toString());
fileWritter.close();
System.out.println("finish");
}
@Override
public void write(int b) throws IOException {
// TODO Auto-generated method stub
}
@Override
public void write(byte[] b) throws IOException {
// TODO Auto-generated method stub
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
// TODO Auto-generated method stub
}
@Override
public void flush() throws IOException {
// TODO Auto-generated method stub
}
@Override
public void close() throws IOException {
// TODO Auto-generated method stub
}
}
class A {
@Override
public String toString() { //用于写文件时,传递类的信息
// TODO Auto-generated method stub
return super.toString()+" this is class A.";
}
}
程序出现什麽问题现象了呢?
为啥写了两遍new File呢
运行报错信息在哪
File类 压根儿没有 new File(File ) 的构造函数;
Objectout 又是什么鬼,ObjectOutput?ObjectOutputStream?