欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java IO流--练习

程序员文章站 2022-03-06 08:21:14
...

1)将若干个Student对象,若干个Teacher对象,写出到d:/0404/a.txt中
2)将该文件中所有Student对象反序列化回来装入List, 所有的Teacher对象反序列化回来转入另外一个List

运用知识点:对象流,文件流

代码:
逻辑—–>建立一个Person类,并实现Serializable。再让Student和Teacher类继承Person。(当然这里没必要,可以直接建立Student和Teacher类)

Person类

package Homework;

import java.io.Serializable;

public class Person implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private String sex;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", sex=" + sex + ", age=" + age + "]";
    }
    public Person(String name, String sex, int age) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    public Person() {
        super();
    }

}

Student类

package Homework;

public class Student extends Person {
    public Student(String name,String sex,int age){
        super(name, sex, age);
    }
    public Student(){}
    @Override
    public String toString() {
        return "Student [getName()=" + getName() + ", getSex()=" + getSex() + ", getAge()=" + getAge() + "]";
    }   
}

Teacher类

package Homework;

public class Teacher extends Person {
    public Teacher(String name,String sex,int age){
        super(name, sex, age);
    }
    public Teacher(){}
    @Override
    public String toString() {
        return "Teacher [getName()=" + getName() + ", getSex()=" + getSex() + ", getAge()=" + getAge() + "]";
    }
}

测试类

package Homework;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class Test1and2 {

    public static void main(String[] args) {
        //在本工程下创建一个文件夹
        File file=new File("Instance");
        file.mkdirs();
        //在本工程下创建的文件夹下创建一个a.txt文件,用来写入对象
        File file2=new File(file,"a.txt");

        //使用对象流写入对象
        ObjectOutputStream oos=null;
        ObjectInputStream ois=null;
        try {
            file2.createNewFile();

            //将若干个Student对象,若干个Teacher对象,写入文件中
            oos=new ObjectOutputStream(new FileOutputStream(file2));
            oos.writeObject(new Student("李四","男",20));
            oos.writeObject(new Student("李四2","男",45));
            oos.writeObject(new Student("李四3","男",38));
            oos.writeObject(new Teacher("张三","男",13));
            oos.writeObject(new Teacher("张三","男",50));
            oos.writeObject(new Teacher("张三","男",34));
            oos.flush();

            //将该文件中所有Student对象反序列化回来装入List
            ois=new ObjectInputStream(new FileInputStream(file2));
            ArrayList<Student> list=new ArrayList<>();
            ArrayList<Teacher> list2=new ArrayList<>();

            //将文件中写入的对象读取出来,并转为对象,存入对应的集合中
            for(int i=0;i<6;i++){
                if(i<=2){
                    Student student=(Student)ois.readObject();
                    list.add(student);
                }else {
                    Teacher teacher=(Teacher)ois.readObject();
                    list2.add(teacher);
                }
            }

            //遍历打印出集合中的对象
            for (Student student : list) {
                System.out.println(student);
            }
            for (Teacher teacher : list2) {
                System.out.println(teacher);
            }


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(oos!=null){
                try {
                    oos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

遍历结果:
Java IO流--练习

3)要求从控制台输入两个整数,之后完成两个整数的加法操作

如果输入整数,求和。
如果输入的不是整数:重新输入,比如ddd

注意:要求使用标准流,不能使用Scanner类

代码:

package com.qf.demo2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {

    public static void main(String[] args) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
         int sum = 0;
        while(true){
            try {
                System.out.println("请输入第一个数:");
                String a = reader.readLine();

                System.out.println("请输入第二个数:");
                String b = reader.readLine();

                //对输入的内容进行正则匹配,判断是否是数字
                boolean flag = a.matches("[0-9]+");
                boolean flag2 = b.matches("\\d+");

                // 如果两次输入的都是数字,那么将两次输入的数据转为整数,并求和,以及退出循环
                if( flag && flag2){
                    sum = Integer.parseInt(a)+Integer.parseInt(b);
                    break;
                }else{
                    // 如果两次输入有一个不是数字,则重新输入,进入循环
                    System.out.println("请重新输入,必须只能包含数字");
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println("输入的两个整数和是:"+sum);
    }
}

运行结果:

Java IO流--练习

上一篇: 第五章

下一篇: 第五章