JavaIO流三个小练习

练习1:

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

(2)在该目录下创建文件hello.txt 并写入内容 hello world!

(3)如果该文件已存在,提示文件已存在。

package com.io.test;

import java.io.*;

/**
 * @author 华子
 * 本章作业:判断e盘下是否有文件夹mytemp,如果没有就创建文件夹
 * 并在该目录下创建hello.txt文件,并写入内容 hello,world!
 * 如果已经存在,则提示该文件已存在
 *
 */
public class Demo01 {
    public static void main(String[] args) throws IOException {

        String directoryPath = "e:\\mytemp";
        File file = new File(directoryPath);
        if (!file.exists()){
            if(file.mkdirs()){
                System.out.println("目录创建成功");
            }else {
                System.out.println("目录创建失败");
            }
        }else {
            System.out.println("目录已经存在");
        }
        String filePath = directoryPath + "\\hua.txt";
        File file2 = new File(filePath);
        if (!file2.exists()){
            if(file2.createNewFile()){
                System.out.println("文件创建成功");
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file2));
                bufferedWriter.write("hello,world!");
                bufferedWriter.close();
            }else{
                System.out.println("文件创建失败");
            }
        }else {
            System.out.println("文件已经存在");
        }
    }
}

练习2:

使用BufferedReader 读取一个文本文件,每行加上一个分号一并输出到屏幕上,并使用转换流制定编码方式

package com.io.test;

import java.io.*;

/**
 * 本章作业2:使用BufferedReader 读取一个文本文件,每行加上一个分号一并输出到屏幕上
 */
public class Demo02 {
    public static void main(String[] args) throws IOException {
        String filePath = "e:\\story.txt";
        String line = "";
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(filePath), "gbk");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        while((line = bufferedReader.readLine()) != null){
            System.out.println(line+";");
        }
        bufferedReader.close();
    }
}

练习3:

(1)编写一个dog.properties,
         name = tom
         age = 5
         color = red
 (2)编写Dog 类,获取一个dog对象,读取dog.properties并对Dog类的属性完成相应的初始化 

(3)将Dog类的属性序列化到dog.dat中。

package com.io.test;


import java.io.*;
import java.util.Properties;

/**
 * @author 华子
 * 本章作业03:编写一个dog.properties,
 * name = tom
 * age = 5
 * color = red
 * 读取dog.properties并完成相应的初始化
 */
public class Demo03 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
//        定义路径
        String filePath = "F:\\IdeaProjects\\JavaSE\\Java基础语法\\src\\dog.properties";
//        创建dog.properties配置文件
        Properties properties = new Properties();
        properties.setProperty("name","tom");
        properties.setProperty("age","5");
        properties.setProperty("color","red");
        properties.store(new FileOutputStream(filePath),null);
        System.out.println("创建dog.properties成功");
        System.out.println("------------------------------");
//        开始初始化Dog类的属性
//        读取配置文件
        properties.load(new FileInputStream(filePath));
        String name = properties.getProperty("name");
        int age = Integer.parseInt(properties.getProperty("age"));
        String color = properties.getProperty("color");
        Dog dog = new Dog(name, age, color);
        System.out.println(dog);

        System.out.println("------------------------------");

        String filePath02 = "e:\\dog.dat";
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath02));
        oos.writeObject(dog);
        oos.close();
        System.out.println("Dog类属性序列化成功");
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath02));
        Object object = ois.readObject();
        System.out.println(object);
    }
}

运行结果:

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

芝麻干

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

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

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

打赏作者

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

抵扣说明:

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

余额充值