各位,我想问一下为什么我的ObjectInputStream里read()会等于-1啊!!import java.io.*;
import java.util.*;public class TestStream
{
public static void main(String args[]) throws Exception//main function to create the ObjectStream
{
Student[] students = {new Student("liuzesen","3110005806"),
new Student("liuzebiao","3110005807"),
new Student("liuzebin","3110005808")};
String str = "Object.txt";
writeObjectToFile(students,str);
/*
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Object.txt"));
Student ss = (Student)ois.readObject();
System.out.printf("%s%s",ss.getName(),ss.getNum());
ois.close();
*/
Student[] sss = readObjectFile(str);
for(Student st: sss)
{
System.out.printf("%s%s",st.getNum(),st.getName());
}
}
public static void writeObjectToFile(Object[] object,String filename)//a function to add Object
{
try
{
int index = 1;
ObjectOutputStream oot = new ObjectOutputStream(new FileOutputStream(new File(filename)));
for(Object obj: object)
{
System.out.println("" + index + " number is added");//CHECK!!!!!!!!
oot.writeObject(obj);
index++;
}
oot.flush();
oot.close();
}catch(IOException e){
e.printStackTrace();
} }
public static Student[] readObjectFile(String filename)//read the Ojbect Stream
{
List<Student> studentList = new ArrayList<Student>();
System.out.println("aa");//CHECK!!!!
try
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(filename)));
int check = -1;
int index = 1;
System.out.println("" + ois.read());//CHECK!!!!!
try{
while((check = ois.read()) != -1)
{
System.out.println("" + index + " number is read");//CHECK!!!!!!!
studentList.add((Student)ois.readObject());
index++;
}
}catch(ClassNotFoundException e)
{e.printStackTrace();}
ois.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
Student[] stu = new Student[studentList.size()];
return studentList.toArray(stu);
}
}class Student implements Serializable//the Object which is obtained in the Stream
{
private static final long serialVersionUID = 1L;//id
public Student()
{
}
public Student(String name,String num)
{
this.name = name;
this.num = num;
}
public String getName()
{
return name;
}
public String getNum()
{
return num;
}
private String name = null;
private String num = null;
}
import java.util.*;public class TestStream
{
public static void main(String args[]) throws Exception//main function to create the ObjectStream
{
Student[] students = {new Student("liuzesen","3110005806"),
new Student("liuzebiao","3110005807"),
new Student("liuzebin","3110005808")};
String str = "Object.txt";
writeObjectToFile(students,str);
/*
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Object.txt"));
Student ss = (Student)ois.readObject();
System.out.printf("%s%s",ss.getName(),ss.getNum());
ois.close();
*/
Student[] sss = readObjectFile(str);
for(Student st: sss)
{
System.out.printf("%s%s",st.getNum(),st.getName());
}
}
public static void writeObjectToFile(Object[] object,String filename)//a function to add Object
{
try
{
int index = 1;
ObjectOutputStream oot = new ObjectOutputStream(new FileOutputStream(new File(filename)));
for(Object obj: object)
{
System.out.println("" + index + " number is added");//CHECK!!!!!!!!
oot.writeObject(obj);
index++;
}
oot.flush();
oot.close();
}catch(IOException e){
e.printStackTrace();
} }
public static Student[] readObjectFile(String filename)//read the Ojbect Stream
{
List<Student> studentList = new ArrayList<Student>();
System.out.println("aa");//CHECK!!!!
try
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(filename)));
int check = -1;
int index = 1;
System.out.println("" + ois.read());//CHECK!!!!!
try{
while((check = ois.read()) != -1)
{
System.out.println("" + index + " number is read");//CHECK!!!!!!!
studentList.add((Student)ois.readObject());
index++;
}
}catch(ClassNotFoundException e)
{e.printStackTrace();}
ois.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
Student[] stu = new Student[studentList.size()];
return studentList.toArray(stu);
}
}class Student implements Serializable//the Object which is obtained in the Stream
{
private static final long serialVersionUID = 1L;//id
public Student()
{
}
public Student(String name,String num)
{
this.name = name;
this.num = num;
}
public String getName()
{
return name;
}
public String getNum()
{
return num;
}
private String name = null;
private String num = null;
}
解决方案 »
- 请教高手怎样实现 MzTreeView2.0 添加,删除,修改??或相关树菜单的例子,谢谢
- 不同系统下文件的读取问题!!求救!
- java高手请进,请教个问题
- 关于克隆(急急急)
- 时...间...问...题...请问怎样才能得到一个干净的日期值 2006-03-04 14:06:26 -> 2006-03-04
- 如何发UDP包?
- 那位大哥帮帮我阿------一个简单的java编译!!!!!!!!!!!
- 如何将字符窜中的某个字符删除??
- 请教各位高手一个简单的问题----关于常量????
- servlet访问路径的问题
- 声明String 字符 和new String 的区别是什么.?
- 请教SSH2怎么用配置方式注入hibernate和struts2???
try{
Student s = (Student)ois.readObject();
System.out.println("" + s.getName());//CHECK!!!!!
}catch(Exception e){
e.printStackTrace();
}
ois.skip(18);
//因为第一个对象站了 18个字节,这样就可去出来第二个对象了
try{
Student s = (Student)ois.readObject();
System.out.println("" + s.getName());//CHECK!!!!!
}catch(Exception e){
e.printStackTrace();
}