package Lesson13;
import java.util.List;
import java.util.Collections;
import java.util.Iterator;
import java.util.ArrayList;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ListIterator;class Student implements Comparable<Student>,Serializable
{
private int sId;
private String name;
private float score;
public Student(int sId,String name,float score)
{
this.sId=sId;
this.name=name;
this.score=score;
}
public String toString()
{
return "学号:"+this.sId+"\t"+"姓名:"+this.name+"\t"+"成绩:"+this.score;
}
public boolean equals(Object obj)
{
if(this==obj)
{
return true;
}
if(!(obj instanceof Student))
return false;
Student stu=(Student) obj;
if((this.sId==stu.sId&&this.name.equals(stu.name)&&this.score==stu.score))
return true;
else
return false;
}
public int hashCode()
{
return this.name.hashCode()*this.sId*(int)this.score;
}
public int compareTo(Student stu)
{
if(this.sId>stu.sId)
return 1;
else if(this.sId<stu.sId)
{
return -1;
}else
return 0;
}
public int getSid()
{
return this.sId;
}
public String getName()
{
return this.name;
}
public float getScore()
{
return this.score;
}
}
public class StudentDe01
{
public static void main(String[] args)throws Exception
{
List<Student> listall=null;
listall=new ArrayList<Student>();
Operate.add(listall);
Operate.printList(listall);
}
}
class Operate
{
public static void add(List< Student> listall)
{


listall.add(new Student(1001,"学生A",98.5f));
listall.add(new Student(1005,"学生B",96.5f));
listall.add(new Student(1004,"学生C",95.5f));
listall.add(new Student(1007,"学生E",94.5f));
listall.add(new Student(1000,"学生D",90.5f)); }
public static void printList(List<Student> listall)throws Exception
{
FileOperate fop=new FileOperate("学生.txt");
Collections.sort(listall);
Iterator<Student> iter=listall.iterator();
while(iter.hasNext())
{
Student st=(Student)iter.next();
System.out.println(st+"、");
fop.putfile(st);
}
System.out.println("开始:");
fop.infile();

}
}
package Lesson13;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.RandomAccessFile;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
public class FileOperate
{
int count=0;
private File file=null;
public FileOperate(String fileName)
{

file=new File("d:"+File.separator+fileName);
if(!file.exists())
{
try
{
file.createNewFile();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public void putfile(Student stu)
{
count++;
ObjectOutputStream oos=null;
OutputStream out=null;
try
{
out=new FileOutputStream(file,true);
}
catch (Exception e)
{
e.printStackTrace();
}

try
{
oos=new ObjectOutputStream(out);
}
catch (Exception e)
{
e.printStackTrace();
}

try
{
oos.writeObject(stu);
}
catch (Exception e)
{
e.printStackTrace();
}

try
{
oos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void infile()
{

//ObjectInputStream ios=null;
InputStream inp=null;
try
{
inp=new FileInputStream(file);
}
catch (Exception e)
{
e.printStackTrace();
}

byte b[]=new byte[2048];
int i=0;
int j=0;
try
{
while((b[i]=(byte)inp.read())!=-1)
{
j++;
}
}
catch (Exception e)
{
e.printStackTrace();
}

System.out.println(b+","+j);
/*try
{
inp=new FileInputStream(file);
}
catch (Exception e)
{
e.printStackTrace();
}

try
{
ios=new ObjectInputStream(inp);
}
catch (Exception e)
{
e.printStackTrace();
}

Student st=null;
int len=0;
for(int i=1;i<=count;i++)
{
try
{
len=ios.available();
st=(Student)ios.readObject();
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println(st);
}
*/
}
}

解决方案 »

  1.   

    给你一个更加简单的方法存储你的这些数据,直接将List作为对象写到文件里面就可以了
    import java.util.List;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.ArrayList;
    import java.io.Serializable;class Student implements Comparable<Student>, Serializable {
    private int sId;
    private String name;
    private float score; public Student(int sId, String name, float score) {
    this.sId = sId;
    this.name = name;
    this.score = score;
    } public String toString() {
    return "学号:" + this.sId + "\t" + "姓名:" + this.name + "\t" + "成绩:"
    + this.score;
    } public boolean equals(Object obj) {
    if (this == obj) {
    return true;
    }
    if (!(obj instanceof Student))
    return false;
    Student stu = (Student) obj;
    if ((this.sId == stu.sId && this.name.equals(stu.name) && this.score == stu.score))
    return true;
    else
    return false;
    } public int hashCode() {
    return this.name.hashCode() * this.sId * (int) this.score;
    } public int compareTo(Student stu) {
    if (this.sId > stu.sId)
    return 1;
    else if (this.sId < stu.sId) {
    return -1;
    } else
    return 0;
    } public int getSid() {
    return this.sId;
    } public String getName() {
    return this.name;
    } public float getScore() {
    return this.score;
    }
    }public class StudentDe01 {
    public static void main(String[] args) throws Exception {
    List<Student> listall = null;
    listall = new ArrayList<Student>();
    Operate.add(listall);
    Operate.printList(listall);
    }
    }class Operate {
    public static void add(List<Student> listall) { listall.add(new Student(1001, "学生A", 98.5f));
    listall.add(new Student(1005, "学生B", 96.5f));
    listall.add(new Student(1004, "学生C", 95.5f));
    listall.add(new Student(1007, "学生E", 94.5f));
    listall.add(new Student(1000, "学生D", 90.5f)); } public static void printList(List<Student> listall) throws Exception {
    FileOperate fop = new FileOperate("学生.txt");
    Collections.sort(listall);
    Iterator<Student> iter = listall.iterator();
    fop.putFile(listall);
    // while (iter.hasNext()) {
    // Student st = iter.next();
    //// System.out.println(st + "、");
    // fop.putFile(st);
    // }
    System.out.println("开始:");
    fop.inFile(); }}import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.ObjectOutputStream;
    import java.io.ObjectInputStream;
    import java.util.List;public class FileOperate {
    int count = 0;
    private File file = null; public FileOperate(String fileName) { file = new File("d:" + File.separator + fileName);
    if (!file.exists()) {
    try {
    file.createNewFile();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    } public void putFile(List<Student> stuList) {
    // count++;
    ObjectOutputStream oos = null;
    OutputStream out = null;
    try {
    out = new FileOutputStream(file);//放到文件尾部
    } catch (Exception e) {
    e.printStackTrace();
    } try {
    oos = new ObjectOutputStream(out);
    } catch (Exception e) {
    e.printStackTrace();
    } try {
    oos.writeObject(stuList);
    } catch (Exception e) {
    e.printStackTrace();
    } try {
    oos.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    } public void inFile() { // ObjectInputStream ios=null;
    InputStream inp = null;
    ObjectInputStream ois=null;
    try {
    inp = new FileInputStream(file);
    ois = new ObjectInputStream(inp);
    } catch (Exception e) {
    e.printStackTrace();
    }

    try {
    List<Student> stuList=(List<Student>)ois.readObject();
    for(Student s:stuList){
    System.out.println("输出"+s);
    }
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    } catch (ClassNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }// for(int i=0;i<count;i++){
    // try {
    // Student s=(Student)ois.readObject();
    // System.out.println(s);
    // } catch (IOException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // } catch (ClassNotFoundException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // }
    try {
    ois.close();
    inp.close();
    } catch (IOException e) {

    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    // System.out.println(b + "," + j); /*
     * try { inp=new FileInputStream(file); } catch (Exception e) {
     * e.printStackTrace(); }
     * 
     * try { ios=new ObjectInputStream(inp); } catch (Exception e) {
     * e.printStackTrace(); }
     * 
     * Student st=null; int len=0; for(int i=1;i<=count;i++) { try {
     * len=ios.available(); st=(Student)ios.readObject(); } catch (Exception
     * e) { e.printStackTrace(); } System.out.println(st); }
     */ }}
      

  2.   


    import java.io.*;
    import java.util.*;public class TestStudentVsFile {    private static List ts = new ArrayList();    private static <T> void addT(T t) {
    if (t != null)
        ts.add(t);
        }    private static <T> void writeToFile(Collection<T> collection,
        String fileName) {// 将集合中的对象 写入文件
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(new FileOutputStream(fileName));
        Iterator<T> iterator = collection.iterator();
        while (iterator.hasNext()) {
    T t = (T) iterator.next();
    oos.writeObject(t);
    oos.flush();
        }
        oos.writeObject(null);// //写入结束标志方便读取(如果不写入,在读取的时候无法定位读取结束);
        oos.flush();
    } catch (Exception e) {
    } finally {
        try {
    if (oos != null)
        oos.close();
        } catch (Exception e) {
        }
    }
        }    private static <T> Collection readFromFile(String fileName) {// 从文件中读取对象
        // 返回一个集合
    ObjectInputStream ois = null;
    Collection<T> collection = new ArrayList<T>();
    try {
        ois = new ObjectInputStream(new FileInputStream(fileName));
        Object tempObject = null;
        T t = null;
        while ((tempObject = ois.readObject()) != null) {
    t = (T) tempObject;
    collection.add(t);
        }
        return collection;
    } catch (Exception e) {
    } finally {
        try {
    if (ois != null)
        ois.close();
        } catch (Exception e) {
        }
    }
    return null;
        }
        public static void main(String[] args) throws Exception {
    addT(new Student(1001, "学生A", 98.5f));
    addT(new Student(1005, "学生B", 96.5f));
    addT(new Student(1004, "学生C", 95.5f));
    addT(new Student(1007, "学生E", 94.5f));
    addT(new Student(1000, "学生D", 90.5f)); Collections.sort(ts);// 输入到文件前先排序 writeToFile(ts, "c:/test.txt");
    ArrayList<Student> tempStudents = (ArrayList<Student>) readFromFile("c:/test.txt");
    for (int i = 0; i < tempStudents.size(); i++) {// 输出从文件中输出的数据
        System.out.println(tempStudents.get(i));
    }
        }
    }// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    class Student implements Comparable<Student>, Serializable {
        private int id;
        private String name;
        private float score;    public Student(int sId, String name, float score) {
    this.id = sId;
    this.name = name;
    this.score = score;
        }    public String toString() {
    return "学号:" + this.id + "\t" + "姓名:" + this.name + "\t" + "成绩:"
    + this.score;
        }    public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (!(obj instanceof Student))
        return false;
    Student stu = (Student) obj;
    if ((this.id == stu.id && this.name.equals(stu.name) && this.score == stu.score))
        return true;
    else
        return false;
        }    public int hashCode() {
    return this.name.hashCode() * this.id * (int) this.score;
        }    public int compareTo(Student stu) {
    if (this.id > stu.id)
        return 1;
    else if (this.id < stu.id) {
        return -1;
    } else
        return 0;
        }    public int getId() {
    return this.id;
        }    public String getName() {
    return this.name;
        }    public float getScore() {
    return this.score;
        }
    }
    /*output: 
     学号:1000 姓名:学生D 成绩:90.5 
     学号:1001 姓名:学生A 成绩:98.5 
     学号:1004 姓名:学生C 成绩:95.5
     学号:1005 姓名:学生B 成绩:96.5 
     学号:1007 姓名:学生E 成绩:94.5
     */