IO流练习

之前我们博客所讲的IO流到这里就告一段落,我们来做一些练习结束IO流。

练习一:

  1. 在判断d盘下是否有文件夹mytmep,如果没有就创建mytemp。
  2. 在d:\111\mytemp 目录下,创建文件hello.txt
  3. 如果hello.txt 已经存在,提示该文件已经存在,就不要再重复创建了。
  4. 并且在hello.txt 文件中,写入hello,world~。
public class HomeWork01 {
    public static void main(String[] args) throws IOException {
        String directoryPath = "d:\\111\\mytemp";
        File file = new File(directoryPath);
        if(!file.exists()){
            //创建
            if(file.mkdirs()){
                System.out.println("创建 "+ directoryPath+ "创建成功");
            }else{
                System.out.println("创建 "+ directoryPath+ "创建失败");
            }
        }

        String filePath = directoryPath +"\\hello.txt";
        file = new File(filePath);
        if(!file.exists()){
            //创建文件
            if (file.createNewFile()){
                System.out.println(filePath+"创建成功~");
                //如果文件存在,我们就是用BufferedWriter 字符输入流写入文件。
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
                bufferedWriter.write("hello,world~~~");
                bufferedWriter.close();
            }else{
                System.out.println(filePath+"创建失败~");
            }
        }else{
            System.out.println(filePath + " 已经存在,不在重复创建。。。");
        }
    }
}

练习二:

要求:使用BufferedReader读取一个文本文件,为每行加上行号,再连同内容一并输出到屏幕上。如果修改了文件的编码,出现中文乱码。

public class HomeWork02 {
    public static void main(String[] args) {
        String filePath = "d:\\111\\story.txt";
        BufferedReader br = null;
        String line = "";
        int lineNum = 0;
        try {
            br =new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"gbk"));
            while ((line = br.readLine()) != null){
                System.out.println(++lineNum +line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (br != null){
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

练习三:

  1. 要编写一个dog.properties

name=tom

age=5

color=red

  1. 编写Dog 类(name,age,color) 创建一个dog对象,读取dog.properties 用相应的内容完成属性初始化,并输出。
  2. 将创建的Dog 对象,序列化到文件dog.dat文件。
public class HomeWork03 {
    public static void main(String[] args) throws IOException {
        String filePath = "src\\com\\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 = "d:\\111\\dog.dat";
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(serFilePath));
        oos.writeObject(dog);
        oos.close();
        System.out.println("dog对象实例化完成!");
    }

    @Test
    public void m1() throws IOException, ClassNotFoundException {
        String serFilePath = "d:\\111\\dog.dat";
        ObjectInputStream osi = new ObjectInputStream(new FileInputStream(serFilePath));
        Dog dog = (Dog)osi.readObject();
        System.out.println("====反序列化后 dog");
        System.out.println(dog);
        osi.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 + '\'' +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值