【Day09-练习】

1.1 字符流

利用字符流将一个文本文件复制到E:盘下,例如:D:/1.txt复制到E:/2.txt

请使用字符流:FileReader和FileWriter实现

public class Test1 {
    public static void main(String[] args) throws Exception {
        try (
                FileReader reader = new FileReader("D:/1.txt");
                FileWriter writer = new FileWriter("E:/2.txt")
        ) {
            char[] chars = new char[256];
            int len;
            while ((len = reader.read(chars)) != -1) {
                writer.write(chars, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

1.2 字节缓冲流

利用字节缓冲流将一个视频文件复制到E:盘下,例如:D:/1.mp4复制到E:/2.mp4

请使用字节缓冲流:BufferedInputStream和BufferedOutputStream实现

public class Test2 {
    public static void main(String[] args) throws Exception {
        try (
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:/1.mp4"));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:/2.mp4"))
        ) {
            byte[] bytes = new byte[1024];
            int len;
            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

 

1.3 字符缓冲流

利用字符缓冲流将一个文件文件复制到E:盘下,例如:D:/1.txt复制到E:/2.txt

请使用字符缓冲流:BufferedReader和BufferedWriter实现

public class Test3 {
    public static void main(String[] args) throws Exception {
        try (
                BufferedReader reader = new BufferedReader(new FileReader("D:/1.txt"));
                BufferedWriter writer = new BufferedWriter(new FileWriter("E:/2.txt"))
        ){
            char[] chars = new char[256];
            String str;
            while ((str = reader.readLine()) != null) {
                writer.write(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

1.4 序列化流

请定义Student类,有以下属性:姓名、性别、年龄

请定义main()方法,创建一个Student对象:

Student stu = new Student(“迪丽热巴”,”女”,18);

定义一个序列化对象:ObjectOutputStream,将stu序列化到文件“1.txt”中

创建一个反序列化对象:ObjectInputStream,将文件”1.txt”的对象反序列化到程序中,并打印此对象。

public class Test4 {
    public static void main(String[] args) throws Exception {
        Student stu = new Student("迪丽热巴", "女", 18);
        try (
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day09-code/f-1.txt"));
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day09-code/f-1.txt"))
        ) {
            oos.writeObject(stu);

            System.out.println(ois.readObject());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Student implements Serializable {
    private String name;
    private String gender;
    private int age;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", 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 Student(String name, String gender, int age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    public Student() {
    }
}

你可能感兴趣的:(java,开发语言,学习)