Java 第19章 IO流 课堂练习+本章作业

Buffered流拷贝二进制文件

package com.hspedu.chapter19.outputStream;

import java.io.*;

public class BufferedCopy02 {
    public static void main(String[] args) {
        String srcFilePath = "c:\\bh.jpg";
        String destFilePath = "c:\\hsp.jpg";

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            bis = new BufferedInputStream(new FileInputStream(srcFilePath));
            bos = new BufferedOutputStream(new FileOutputStream(destFilePath));
            
            byte[] buff = new byte[1024];
            int readLen = 0;

            while ((readLen = bis.read(buff)) != -1) {
                bos.write(buff, 0, readLen);
            }
            System.out.println("Copied successfully..");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                bis.close();
                bos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}

创建文件写入文本

在这里插入图片描述

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Homework01 {
    public static void main(String[] args) throws IOException {
        String filePath = "c:\\mydir";
        File file = new File(filePath);
        if (!file.exists()) {
            if (file.mkdirs())
                System.out.println(filePath + " has been created successfully..");
            else
                System.out.println(filePath + " was fail to be created..");
        } else {
            System.out.println(filePath + " has already existed..");
        }
        String destfile = filePath + "\\hello.txt";
        File file1 = new File(destfile);
        if (!file1.exists()) {
            if (file1.createNewFile()) {
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file1));
                bufferedWriter.write("hello, world~~ 韩顺平教育");
                bufferedWriter.close();
                System.out.println(destfile + " has been created successfully..");
            } else {
                System.out.println(destfile + " has already existed..");
            }
        } else {
            System.out.println(destfile + " has already existed..");
        }
    }
}

读取文本文件

在这里插入图片描述

public class Homework02 {
    public static void main(String[] args) throws IOException {
        String filePath = "c:\\hello.txt";
        String line = "";
        int lineNum = 0;
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(++lineNum + " " + line);
        }
        if (bufferedReader != null)
            bufferedReader.close();
    }
}

在这里插入图片描述

public class Homework02 {
    public static void main(String[] args) throws IOException {
        String filePath = "c:\\hello.txt";
        // InputStreamReader指定编码方式
        InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "gbk");
        String line = "";
        int lineNum = 0;
        BufferedReader bufferedReader = new BufferedReader(isr);
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(++lineNum + " " + line);
        }
        if (bufferedReader != null)
            bufferedReader.close();
    }
}

存读Properties文件

在这里插入图片描述

public class Homework03 {
    public static void main(String[] args) throws IOException {
        String filePath = "src\\dog.properties";
        Properties properties = new Properties();
        properties.load(new FileReader(filePath));
        String name = properties.get("name") + ""; //Object -> String
        int age = Integer.parseInt(properties.get("age") + "");// Object -> int
        String color = properties.get("color") + "";//Object -> String

        Dog dog = new Dog(name, age, color);
        System.out.println("===dog对象信息====");
        System.out.println(dog);

        //将创建的Dog 对象 ,序列化到 文件 dog.dat 文件
        String serFilePath = "c:\\dog.dat";
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(serFilePath));
        oos.writeObject(dog);

        //关闭流
        oos.close();
        System.out.println("dog对象,序列化完成...");
    }

    //在编写一个方法,反序列化dog
    @Test
    public void m1() throws IOException, ClassNotFoundException {
        String serFilePath = "e:\\dog.dat";
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(serFilePath));
        Dog dog = (Dog)ois.readObject();

        System.out.println("===反序列化后 dog====");
        System.out.println(dog);

        ois.close();

    }
}

class Dog implements  Serializable{
    private String name;
    private int age;
    private String color;

    public Dog(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }
}
  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 编写程序从文件中读取数据,并输出到控制台。 ```java import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReadFromFile { public static void main(String[] args) { try { File file = new File("data.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { System.out.println(scanner.nextLine()); } scanner.close(); } catch (FileNotFoundException e) { System.out.println("File not found."); } } } ``` 2. 编写程序从控制台读取数据,并将数据写入文件。 ```java import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class WriteToFile { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter data: "); String data = scanner.nextLine(); scanner.close(); try { File file = new File("output.txt"); FileWriter writer = new FileWriter(file); writer.write(data); writer.close(); } catch (IOException e) { System.out.println("Error writing to file."); } } } ``` 3. 编写程序从一个文件中复制数据到另一个文件。 ```java import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class CopyFile { public static void main(String[] args) { try { File inputFile = new File("input.txt"); File outputFile = new File("output.txt"); FileInputStream inputStream = new FileInputStream(inputFile); FileOutputStream outputStream = new FileOutputStream(outputFile); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, length); } inputStream.close(); outputStream.close(); } catch (FileNotFoundException e) { System.out.println("File not found."); } catch (IOException e) { System.out.println("Error copying file."); } } } ``` 4. 编写程序读取一个目录下的所有文件,并输出文件名和文件大小。 ```java import java.io.File; public class ListFiles { public static void main(String[] args) { File directory = new File("."); File[] files = directory.listFiles(); for (File file : files) { if (file.isFile()) { System.out.println(file.getName() + " (" + file.length() + " bytes)"); } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值