62,621
社区成员
发帖
与我相关
我的任务
分享
import java.io.*;
public class TestIO {
private final static int COUNT = 3;
private final static String fileName = "01.dat";
public static void input() {
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new FileInputStream(fileName));
for (int i = 0; i < COUNT; i++) {
int t = in.readInt();
System.out.println(t);
}
in.close();
} catch (FileNotFoundException e) {
System.out.println("file not found");
} catch (IOException e) {
System.out.println("wrong read");
}
}
public static void write() {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new FileOutputStream(fileName));
for (int i = 0; i < COUNT; i++) {
out.writeInt(i);
}
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
write();
input();
}
}