【Java学习笔记】66 - 本章作业(IO流)

本文介绍了Java编程中如何使用File类和FileWriter进行文件操作,处理流的应用,以及Properties类用于读写配置文件和对象序列化的示例。
摘要由CSDN通过智能技术生成

项目代码

https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_Chapter19/src/com/yinhai/homework

目录

项目代码

1.使用File类和FileWriter类

2. 使用处理流

3.使用Properties类,使用对象流


1.使用File类和FileWriter类

(1)在判断e盘下是否有文件夹mytemp,如果没有就创建mytemp

public class Homework01 {
    public static void main(String[] args) throws FileNotFoundException {
        String path = "e:\\test\\mytemp";
        File file = new File(path);
        if(!file.exists()){
            file.mkdirs();
        }else{
            System.out.println("已存在");
        }
    }
}

        

(2)在e:\\mytemp目录下,创建文件hello.txt

记得关闭该字符流

FileWriter fileWriter =null;
        try {
            fileWriter = new FileWriter(path + "\\test.txt",true);
            fileWriter.write("hello,world\n");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            fileWriter.close();
        }

(3)如果hello.txt已经存在,提示该文件已经存在,就不要再重复创建了

//3)如果hello.txt已经存在,提示该文件已经存在,就不要再重复创建了
        File file1 = new File(path,"\\hello.txt");
        FileWriter fileWriter1 =null;

        try {
            if(!file1.exists()) {
                fileWriter1 = new FileWriter(file1);
                fileWriter1.write("hello,world\n");
            }else{
                System.out.println("已存在");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileWriter1 != null) {
                fileWriter1.close();
            }
        }

2. 使用处理流

要求:使用BufferedReader读取一 个文本文件,为每行加上行号,再连同内容一并输出到屏幕上。
 

3.使用Properties类的使用

(1)要编写一个dog.properties

        name= tom

        age= 5

        color= red

(2)编写Dog类(name,age.color)创建一 个dog对象, 读取dog.properties 用相应的内容完
成属性初始化,并输出

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

3.使用Properties类,使用对象流

(1)要编写一个dog.properties

        name = tom

        age=5

        color= red

(2)编写Dog类(name,age,color)创建一个dog对象, 读取dog.properties 用相应的内容完成属性初始化,并输出

(3)将创建的Dog对象,序列化到文件dog.dat文件

public class Homework03 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Properties properties = new Properties();
        properties.setProperty("name","tom");
        properties.setProperty("age","50");
        properties.setProperty("color","red");
        properties.store(new FileOutputStream("src\\dog.properties"),null);
        Dog dog = new Dog(properties);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("e:\\test\\dog.dat"));
        objectOutputStream.writeObject(dog);
        objectOutputStream.close();//需要关闭或者刷新才会写入成功
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("e:\\test\\dog.dat"));
        Object dogInputStream = objectInputStream.readObject();
        Dog dog1 = (Dog)dogInputStream;
        System.out.println(dog1);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yinhai1114

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值