import java.io.*;
public class TestObject {
public TestObject() {
}
public static void main(String[] args) throws FileNotFoundException,IOException,ClassNotFoundException{
try{ Staff a=new Staff();
a.t=2;
Staff b=new Staff();
b.t=3; FileOutputStream fos=new FileOutputStream("../test.txt",true);
BufferedOutputStream bos=new BufferedOutputStream(fos);
ObjectOutputStream oos=new ObjectOutputStream(bos);
System.out.println("-1");
oos.writeObject(a);
System.out.println(a);
oos.writeObject(b);
System.out.println(b);
FileInputStream fis=new FileInputStream("../test.txt");
System.out.println("5");
BufferedInputStream bis=new BufferedInputStream(fis);
System.out.println("6");
ObjectInputStream ois=new ObjectInputStream(bis);
System.out.println("0");
System.out.println(ois.readObject());
System.out.println("1");
System.out.println(ois.readObject());
System.out.println("2"); oos.flush();
bos.flush();
fos.flush();
oos.close();
bos.close();
fos.close();
fis.close();
bis.close();
ois.close();
}catch(EOFException sd)
{
System.out.println("Exception");
}
}}
class Staff implements Serializable
{
int t=0;
}
//运行结果:-1
//Staff@1a758cb
//Staff@1b67f74
//5
//6
//Exception
//为什么从0开始后面的都没输出了。是什么问题。我想测试的是作为Object 读出读入文件
public class TestObject {
public TestObject() {
}
public static void main(String[] args) throws FileNotFoundException,IOException,ClassNotFoundException{
try{ Staff a=new Staff();
a.t=2;
Staff b=new Staff();
b.t=3; FileOutputStream fos=new FileOutputStream("../test.txt",true);
BufferedOutputStream bos=new BufferedOutputStream(fos);
ObjectOutputStream oos=new ObjectOutputStream(bos);
System.out.println("-1");
oos.writeObject(a);
System.out.println(a);
oos.writeObject(b);
System.out.println(b);
FileInputStream fis=new FileInputStream("../test.txt");
System.out.println("5");
BufferedInputStream bis=new BufferedInputStream(fis);
System.out.println("6");
ObjectInputStream ois=new ObjectInputStream(bis);
System.out.println("0");
System.out.println(ois.readObject());
System.out.println("1");
System.out.println(ois.readObject());
System.out.println("2"); oos.flush();
bos.flush();
fos.flush();
oos.close();
bos.close();
fos.close();
fis.close();
bis.close();
ois.close();
}catch(EOFException sd)
{
System.out.println("Exception");
}
}}
class Staff implements Serializable
{
int t=0;
}
//运行结果:-1
//Staff@1a758cb
//Staff@1b67f74
//5
//6
//Exception
//为什么从0开始后面的都没输出了。是什么问题。我想测试的是作为Object 读出读入文件
bos.flush();
fos.flush();
oos.close();
bos.close();
fos.close();放到FileInputStream fis=new FileInputStream("../test.txt")前,且其实直接close掉oos就可以了
oos.flush(); 或者bos.flush(); 一定要放在 5之前;否则 test.txt其实还是空的。
楼主可以调试一下,你会发现 在flush之前, test.txt的大小一直是0。
1. 如果没有锁的保护,最好不好同时打开同一个文件的输入流和输出流,即这样的结构是有问题的:
打开输出流>写内容>打开输入流>读内容>关闭输入流>关闭输出流
或
打开输出流>写内容>打开输入流>读内容>关闭输入流>关闭输出流
正确的代码是:
打开输出流>写内容>flush(可选)>关闭输出流 打开输入流>读内容>关闭输入流
或
打开输入流>读内容>关闭输入流 打开输出流>写内容>flush(可选)>关闭输出流2. 对于包装多层流的操作: 无需每层都flush和close,只需flush和关闭最外层的就可以,如:
oos.flush();
bos.flush();
fos.flush();
oos.close();
bos.close();
fos.close();
应改成
oos.flush();
oos.close();
就可以。针对以上为题,我将你的代码修改如下:import java.io.*;public class TestObject { public TestObject() {
} public static void main(String[] args) throws FileNotFoundException,
IOException, ClassNotFoundException {
try {
Staff a = new Staff();
a.t = 2;
Staff b = new Staff();
b.t = 3; FileOutputStream fos = new FileOutputStream("d:/test.txt", true);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos);
System.out.println("-1");
oos.writeObject(a);
System.out.println(a);
oos.writeObject(b);
System.out.println(b);
//flush并关闭输出流:
oos.flush();
oos.close();
FileInputStream fis = new FileInputStream("d:/test.txt");
System.out.println("5");
BufferedInputStream bis = new BufferedInputStream(fis);
System.out.println("6");
ObjectInputStream ois = new ObjectInputStream(bis);
System.out.println("0");
System.out.println(ois.readObject());
System.out.println("1");
System.out.println(ois.readObject());
System.out.println("2");
//flush并关闭输入流:
ois.close();
} catch (EOFException sd) {
System.out.println("Exception");
} }
}class Staff implements Serializable {
int t = 0;
}
package staffInformationManagement;import java.io.*;
import java.util.*;class IOTxt implements readWriteinterface{
static ObjectInputStream i=null;
static BufferedInputStream in=null;
private ObjectInputStream readAStaff() throws IOException, ClassNotFoundException
{ FileInputStream omg=new FileInputStream("../records.txt");
in=new BufferedInputStream(omg);
i=new ObjectInputStream(in);
return i;
}
public ArrayList read() throws IOException, ClassNotFoundException
{
ArrayList<Staff> staff=new ArrayList<Staff>();
//System.out.println("1");
Staff astaff=null;
ObjectInputStream oi=null;
oi=readAStaff();
try{
while(true)
{ // System.out.println("1");
// System.out.println("2");
Object o=oi.readObject();
// 处理已读出的对象o;
// System.out.println("3");
staff.add((Staff)o);
//System.out.println(((Staff)o).employeePayrollNumber);
System.out.println("fdsfdsf");//这个有输出
System.out.println((Staff)(oi.readObject()));//这个不会打印。
}
}catch(EOFException e){
System.out.println("5"); //已从流中读完。这个不会运行到。
}
finally{
// 流的关闭。
oi.close();
i.close();
return staff;
}
}
public void write(Staff staff)
{
try{
FileOutputStream fout=new FileOutputStream("../records.txt",true);
BufferedOutputStream out=new BufferedOutputStream(fout);
ObjectOutputStream o=new ObjectOutputStream(out);
o.writeObject(staff);
System.out.println(staff.departmentNumber);
o.flush();
o.close();
System.out.println("\r\n Record Saved in txt");
}
catch(Exception ie)
{
ie.printStackTrace();
}
}
}