回 帖 发 新 帖 刷新版面

主题:[讨论]该错题,你知道吗?

请各位高手帮我改一下代码
大体思路看程序
package jh;

import java.io.*;




    class Student{
        String id;
        String age;
        String department;
        String name;
        Student(String id,String age,String department,String name){
            this.id = id;
            this.age = age;
            this.department = department;
            this.name = name;
        }
    }
    /**
     * @param args
     */
    
    public class TestInput {
    public static void main(String[] args) {
        FileInputStream fi = null;
        ObjectInputStream si = null;
        Student stu = null;
        
            try {
                fi = new FileInputStream("text.txt");
                si = new ObjectInputStream(fi);
                stu = (Student) si.readObject();
                si.close();
                System.out.print("student stu : id " + stu.id);
                System.out.print("student stu : age " + stu.age);
                System.out.print("student stu : department" + stu.department);
                System.out.print("student stu : name" + stu.name);
            }catch(StreamCorruptedException a)
            {
                a.printStackTrace();
            }
            catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }            
        
        
    }

}

回复列表 (共1个回复)

沙发

//1 Student 要实现接口 Serializable 表示此对象可以序列化。
要先将Student的对象写入一个文件然后才能从该文件中读取改对象。

import java.io.*;




    class Student implements  Serializable{
        String id;
        String age;
        String department;
        String name;
        Student(String id,String age,String department,String name){
            this.id = id;
            this.age = age;
            this.department = department;
            this.name = name;
        }
    }
    /**
     * @param args
     */
    
 class TestInput {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        Student s1 = null;
        
            try {
                s1 = new Student("001","20","AAA","AAA");
                fos = new FileOutputStream("text.txt");
                oos = new ObjectOutputStream(fos);
                oos.writeObject(s1);
                oos.close();
                fis = new FileInputStream("text.txt");
                ois = new ObjectInputStream(fis);
                Student s2 = (Student) ois.readObject();
                ois.close();
                System.out.print("student stu : id " + s2.id);
                System.out.print("student stu : age " + s2.age);
                System.out.print("student stu : department" + s2.department);
                System.out.print("student stu : name" + s2.name);
            }catch(StreamCorruptedException a)
            {
                a.printStackTrace();
            }
            catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }            
        
        
    }

}

我来回复

您尚未登录,请登录后再回复。点此登录或注册