“21天好习惯”第一期-20

21天养成好习惯_第二十天

文件字节流:

在这里插入图片描述

直接上代码对比字节流和字符流的细微区别
字符流
package IO_Study;

import java.io.*;


/**
 * 使用FileReader和FileWrite复制文本文件,不能复制图片以及所有二进制文件夹(应为二进制--> 字符会出现乱码)
 * 使用字节流FileInputStream|FileOutputStream 可以复制任意文件
 */
public class FileStreamCopyDemo {
    public static void main(String[] args) throws IOException {
        //1.创建FileReader FileWrite
        FileReader fr = new FileReader("buffer.txt");
        FileWriter fw = new FileWriter("buffer2.txt");
        //2.读写
        int data = 0;
        int num = 0;
        //2.1一个字符一个字符读
//        while ((data = fr.read()) != -1){
//            num++;
//            fw.write(data);
//        }


        //2.2利用缓冲区(减少次数, 更加高效)
        char[] buf = new char[1024];//注意这里是char
        int count = 0;
        while ((count=fr.read(buf))!=-1){
            System.out.println(new String(buf,0,count));
            num++;//只执行一次
        }
        System.out.println(num);
        //3.关闭(内置flush)
        fw.close();
        fr.close();
        System.out.println("复制完毕");
    }
}

字节流
package IO_Study;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

//利用文件字节流实现文件的复制
public class FileCopyDemo {
    public static void main(String[] args) throws IOException {
        //1.创建流
        //1.1 字节输入流
        FileInputStream fis = new FileInputStream("d:\\1.png");
        //1.2 字节输出流
        FileOutputStream fos = new FileOutputStream("d:\\2.png");
        //2.一边读一边写
        byte[] buf = new byte[1024];
        int count = 0;
        while ((count=fis.read(buf))!=-1){
            fos.write(buf,0,count);
        }

        //3.关闭
        fis.close();
        fos.close();
        System.out.println("执行完毕");
    }
}

总结
  1. 字节流可以复制任意格式文件,而字符流只能复制文本文件
  2. 利用缓冲区可提高写读效率, 注意字节流的缓冲区要定义为byte[]
    字符流缓冲区要定义为char[]
File类

在这里插入图片描述

Property集合的相关知识
package IO_Study;

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

public class PropertyDemo {
    public static void main(String[] args) throws IOException {
        //1.创建集合
        Properties properties = new Properties();
        //2.添加数据 (其key和value都是String类型 )
        properties.setProperty("username","zhangsan");
        properties.setProperty("age","20");
        System.out.println(properties.toString());
        //3.遍历
        //3.1 -----keySet---
        //3.2------entrySet---
        //3.3------stringPropertyNames()---
        Set<String > pronames = properties.stringPropertyNames();
        //将其所有元素的key都存储到这个集合中
        for (String proname : pronames) {
            System.out.println(proname + "--->" + properties.getProperty(proname));
        }
        //4.和流有关的方法
        PrintWriter printWriter = new PrintWriter("d:\\print.txt");
        properties.list(printWriter);//将结果以文件的形式输出
        printWriter.close();

        //4.2 store方法
        FileOutputStream fileOutputStream = new FileOutputStream("store.properties");
        properties.store(fileOutputStream,"Mycomment");
        fileOutputStream.close();

        //4.3 load方法加载
        Properties properties1 = new Properties();
        FileInputStream fileInputStream = new FileInputStream("store.properties");
        properties1.load(fileInputStream);
        fileInputStream.close();
        System.out.println(properties1.toString());
    }
}

运行结果:

{age=20, username=zhangsan}
age--->20
username--->zhangsan
{age=20, username=zhangsan}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值