package Zy; import java.io.Serializable; public class Student implements Serializable{ private static final long serialVersionUID = 55645321545L; private String name; private String gender; private int age; public Student() { super(); } public Student(String name, String gender, int age) { super(); this.name = name; this.gender = gender; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static long getSerialversionuid() { return serialVersionUID; } @Override public String toString() { return "Student [name=" + name + ", gender=" + gender + ", age=" + age + "]"; } }
package Zy; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.sound.sampled.Line; import Zjshuchu.Dog; public class Zy01 { /* 1.用代码实现以下需求 (1)定义学生类,包含姓名(String name),性别(String gender),年龄(int age)三个属性,生成空参有参构造,set和get方法,toString方法 (2)键盘录入6个学员信息(录入格式:张三,男,25),要求有两个相同的信息,将6个学员信息存入到ArrayList集合中 (3)将存有6个学员信息的ArrayList集合对象写入到D:\StudentInfo.txt文件中 (4)读取D:\StudentInfo.txt文件中的ArrayList对象*/ public static void main(String[] args) throws IOException, ClassNotFoundException { //读取 main(); //写入 FileOutputStream fis=new FileOutputStream("d:\java\student.txt"); ObjectOutputStream oos=new ObjectOutputStream(fis); ArrayList<Student> list=new ArrayList<Student>(); Scanner sc=new Scanner(System.in); Scanner sc1=new Scanner(System.in); System.out.println("输入姓名"); String n1=sc.nextLine(); System.out.println("输入性别"); String x1=sc.nextLine(); System.out.println("输入年龄"); int age1=sc1.nextInt(); Student s=new Student(); s.setName(n1); s.setGender(x1); s.setAge(age1); list.add(s); System.out.println("输入姓名"); String n2=sc.nextLine(); System.out.println("输入性别"); String x2=sc.nextLine(); System.out.println("输入年龄"); int age2=sc1.nextInt(); Student s1=new Student(); s.setName(n2); s.setGender(x2); s.setAge(age2); list.add(s1); System.out.println("输入姓名"); String n3=sc.nextLine(); System.out.println("输入性别"); String x3=sc.nextLine(); System.out.println("输入年龄"); int age3=sc1.nextInt(); Student s2=new Student(); s.setName(n3); s.setGender(x3); s.setAge(age3); list.add(s2); System.out.println("输入姓名"); String n4=sc.nextLine(); System.out.println("输入性别"); String x4=sc.nextLine(); System.out.println("输入年龄"); int age4=sc1.nextInt(); Student s3=new Student(); s.setName(n4); s.setGender(x4); s.setAge(age4); list.add(s3); System.out.println("输入姓名"); String n5=sc.nextLine(); System.out.println("输入性别"); String x5=sc.nextLine(); System.out.println("输入年龄"); int age5=sc1.nextInt(); Student s4=new Student(); s.setName(n5); s.setGender(x5); s.setAge(age5); list.add(s4); System.out.println("输入姓名"); String n6=sc.nextLine(); System.out.println("输入性别"); String x6=sc.nextLine(); System.out.println("输入年龄"); int age6=sc1.nextInt(); Student s5=new Student(); s.setName(n6); s.setGender(x6); s.setAge(age6); list.add(s5); oos.writeObject(list); FileInputStream fos=new FileInputStream("d:\java\student.txt"); ObjectInputStream ois=new ObjectInputStream(fos); Student b=(Student)ois.readObject(); System.out.println(b); oos.close(); } public static void main() throws IOException, ClassNotFoundException{ FileInputStream fos=new FileInputStream("d:\java\student.txt"); ObjectInputStream ois=new ObjectInputStream(fos); System.out.println(ois.readObject()); } }