zoukankan      html  css  js  c++  java
  • IO流(02)--属性集、缓冲流、转换流

    属性集【Properties】

    java.util.Properties类继承于Hashtable,用来表示一个持久的属性集。它使用键值结构存储数据,每个键及其对应的值都是一个字符串。

    构造方法
    • ​ public Properties():创建一个空的属性集列表。
    共性的api方法
    • public Object setProperty(String key,String value):保存一对属性。
    • public String getProperty(String key):使用此属性列表中的指定的键搜索对应的值。
    • public Set stringPropertyNames():获取所有键的名称并封装到Set集合中。
    public static void main(String[] args) {
            // 创建属性集对象
            Properties properties = new Properties();
            // 添加键值对元素
            properties.setProperty("name", "abc.txt");
            properties.setProperty("size", "12000");
            properties.setProperty("destination","D:\abc.txt");
            properties.put("data","小孙");
            System.out.println(properties);// {destination=D:abc.txt, name=abc.txt, size=12000}
    
            // 通过键来获取值
            String data = properties.getProperty("data");
            System.out.println(data); // 小孙
            String size = properties.getProperty("size");
            System.out.println(size);// 12000
    
            // 遍历该属性集
            // public Set<String> stringPropertyNames():获取所有键的名称并封装到Set集合中。
            Set<String> keys = properties.stringPropertyNames();
            // 遍历keys
            for (String key : keys) {
                // 通过key获取value
                String value = properties.getProperty(key);
                System.out.println(key + "=" + value);
            }
    
        }
    
    与流相关的方法
    • public void load(InputStream input):从字节输入流中读取键值对

    参数中使用了字节输入流,通过流对象,可以关联到某个文件上,这样既可以加载文件中的数据。文件中的数据的格式:key=value

    例如:
       data=小孙
       size=12000  
       name=abc.txt
    

    代码演示:

    public static void show01() throws IOException {
            // 0.构建一个流对象
            FileReader fr = new FileReader("day29_IO\abc.txt");
            // 1.创建Properties集合
            final Properties properties = new Properties();
            //  2.使用Properties集合中的方法load读取保存在输入流中的数据
            properties.load(fr);
            //  3.遍历Properties集合
            final Set<String> set = properties.stringPropertyNames();
            for (String key : set) {
                // 通过key获取value值
                final String value = properties.getProperty(key);
                System.out.println(key + "=" + value);
            }
            /*
                name=abc.txt
                size=12000
                data=小孙
                目的地=D:abc.txt
             */
        }
    
    • ​ public void store(OutputStream out,String comments):把集合当中数据写入字节输出流中

    可以使用Properties集合当中的方法store,把集合当中的临时数据,持久化写入到硬盘文件中保存。

    代码示例:

    public static void show02() throws IOException {
            // 1. 创建Properties集合对象,添加数据
            final Properties properties = new Properties();
            properties.setProperty("四大名著1","红楼梦");
            properties.setProperty("四大名著2","西游记");
            properties.setProperty("四大名著3", "水浒传");
            properties.setProperty("四大名著4", "三国演义");
            // 2. 创建字节输出流/字符输出流对象,构造方法中绑定需要写入数据的目的地
            final FileWriter fw = new FileWriter("day29_IO\abcd.txt", true);
            // 3. 使用Properties集合中的方法store,把集合当中的临时数据,持久化写入到硬盘当中存储
            properties.store(fw, "si da ming zhu");
            // 4.释放资源。
            fw.close();
    }
    

    缓冲流【Buffered】

    缓冲流我们理解为对原来的使用数组方式进行数据传输的一种增强

    按照类型分为:

    • 字符缓冲流:BufferedReader,BufferedWriter
    • 字节缓冲流:BufferedInputStream,BufferedOutputStream

    缓冲流的基本原理,是在创建流对象的时候,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写数据,减少系统IO操作的次数,减少开销,提高程序的读写的效率。

    字节缓冲流
    构造方法
    • public BufferedInputStream(InputStream input):创建一个新的缓冲输入流
    • public BufferedOutputStream(OutputStream output):创建一个新的缓冲输出流

    代码示例:

    public class Demo02 {
        public static void main(String[] args) throws IOException {
            // 1.创建一个FileOutputStream流对象,构造方法中标的需要绑定写入数据的数据源
            FileInputStream fis = new FileInputStream("day29_IO\one.txt");
            // 2.创建BufferedInputStream对象,构造方法中传递FileInputStream流对象
            BufferedInputStream bis = new BufferedInputStream(fis);
            //3.使用BufferedInputStream对象指定方法read,把数据读取到内存中
            /*int len = 0;
            while ((len=bis.read())!=-1){
                System.out.println((char)len);
            }*/
            byte[]bytes = new byte[1024];
            int len = 0;
            while ((len=bis.read(bytes))!=-1){
                System.out.println(new String(bytes,0,len));
            }
            //4.释放资源
            bis.close();
        }
    }
    

    字符缓冲流

    构造方法

    • public BufferedWriter(Writer out):创建一个 新的字符缓冲输出流

    • public BufferedReader(Reader in):创建一个新的字符缓冲输入流。

    特有方法:

    • BufferedReader: public String readLine():读取整行的文本信息

    • BufferedWriter: public void newLine():写入一行的行分隔符,由系统属性定义换行符号。

    字符缓冲输入流代码演示:

    public static void main(String[] args) throws IOException {
            //1.创建一个字符缓冲输入流对象,构造方法中传入一个字符输入流
            BufferedReader br = new BufferedReader(new FileReader("day29_IO\abc.txt"));
            //2.使用字符缓冲流对象中的read/readLine,
           /* String str = br.readLine();
            System.out.println(str);*/
            //循环结束条件readLine()返回值是null的时候
            String str = null;
            while ((str=br.readLine())!=null){
                System.out.println(str);
            }
            //3.释放资源。
            br.close();
        }
    

    字符缓冲输出流代码演示:

    public static void main(String[] args) throws IOException {
            // 1.创建一个字符缓冲输出流对象,构造方法中传递一个字符输出流
            BufferedWriter bw = new BufferedWriter(new FileWriter("day29_IO\two.txt"));
            //2.调用字符流对象中的writer,把数据写入到内存存储区中
            bw.write("我今天学习了PS,虽然没学会");
            bw.newLine();
            bw.write("3D软件mmd");
            bw.newLine();
            bw.write("c4d");
            // 3.调用字符缓冲输出流对象的flush方法,把你想缓冲区的数据刷新到文件中
            bw.flush();
            //4.释放资源
            bw.close();
        }
    

    练习:

     public static void show02() throws IOException {
            long start = System.currentTimeMillis();
            //1.构建字节缓冲输入流对象
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\Users\Administrator\Desktop\0.jpg"));
            //2.构建个字节缓冲输出流
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\code\java31\day29_IO\0.jpg"));
            //3.用字节缓冲输入流对象中的方法read(byte[] b)读取文件
            byte[]bytes = new byte[1024];
    
            int len = 0;//记录读取到有效字节个数
            while ((len = bis.read(bytes))!=-1){
                //节内容再次写入到目的地文件中,调用write
                bos.write(bytes,0,len);
            }
            bos.close();
            bis.close();
    
            long end= System.currentTimeMillis();
            System.out.println("文件复制耗费的时间为:"+(end-start));
        }
    

    转换流【字节流<-->字符流】

    • 字符编码:
        按照某种规则将字符存储到计算机中,称为编码;反之,将存储在计算机中的二进制数按照某种规则解析显示出来,称为解码。在进行编码或者解码的过程中,我们采用的是同一种规则,才能数据正常,否则,就会导致乱码现象。就是一套自然语言中的字符与二进制数直接的对应规则。

    • 字符集:
        是一个系统可支持的所有字符的集合,包括各国文字,标点符号,图形符号,数字等,也叫编码表。

    计算机中要准确的存储和识别各种文字的字符符号,需要进行字符编码,一套字符集至少有一套字符编码。

    常见的字符编码集有ASCII字符集,GBK字符集、Unicode字符集。

    ASCII字符集:

    • ASCII是基于拉丁字母的一套编码系统,用于显示现代英语。

    • 基本的ASCII字符集,使用7位(bit)表示一个字符,共128个字符。ASCII的扩展字符集使用8位(bit)表示一个字符,共25
      6个字符。

    ISO—8859-1字符集

    • 拉丁码表,别名--Lanlin-1,用于显示欧洲使用的语言,包括荷兰,丹麦,德语,意大利语,西班牙语等。

    • ISO-8859-1使用单字节编码,兼容ASCII编码。

    GB系列符集

    • CB2312:称为简体中文码表里面大概含有7000多个简体汉字,此外数字符号。罗马希腊的字母,日本的假名都编进去了,连在ASCII里的原来就有的数字、标点、字母都统统重新用两个字节编写进去了。

    • GBK:最常用的中文码表。是在原来的GB2312码表基础上进行了扩展。也是使用双字节编码。共收录了21000多个汉字,完全兼容GB2312标准,同时支持繁体汉字以及日韩汉字等。

    • GB18030:最新的中文码表,共收录了7万多个汉字,采用多字节编码,每个字可以由1个字节、2个字节或者是4个字节组成,支持国内少数民族的文字。同时也支持繁体字以及日韩汉字等。

    Unicode字符集:

    • Unicode编码系统为表达任意语言的任意字符而设计的,是业界的一种标准,也称为统一编码,标准万国码表

    • 它最多使用4个字节的数字来表示某个字母、符号、汉字文字,有三种常见的编码方案:UTF-8,UTF-6,UTF-32.

    • UTF-8编码表,用来表示Unicode标准中的任意字符,编码规则:

      1.128个US-ASCII字符,使用的是一个字节编码
      2.拉丁字的字母,需要两个字节编码
      3.大部分常见的汉字,使用的是三个字节编码
      4.其他极少数的辅助字符,采用的四个字节编码。

    编码会引发的问题

    由于编码规则不一致,导致引发乱码现象。

    那么如何读取GBK编码的文件呢?

    InputStreamReader类

    转换流 java.io.InputStreamReader 是Reader的子类,它是从字节流到字符流的桥梁。它读取字节,并使用指定的字符集将其解码为字符。它的字符集可以由名称指定,或者可以使用平台默认的字符集。

    构造方法
    • public InputStreamReader(InputStream in):创建一个使用默认的字符集的字符流。

    • public InputStreamReader(InputStream in,String charseName):创建一个指定字符集的字符流

    代码演示:

     public static void show01() throws IOException {
            //1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
            InputStreamReader isr = new InputStreamReader(new FileInputStream("day29_IO\新建文本文档.txt"), "GBK");
            //2.使用InputStreamReader对象中的方法read读取文件中的信息
            int len = 0;
            while ((len=isr.read())!=-1) {
                System.out.println((char)len);
            }
            //3.释放资源。
            isr.close();
        }
    

    OutStreamWriter类

    转换流java.io.OtputSreamWiter是Writer的子类,它是字符流到字节流的桥梁。使用指定的字符集将字符编码为字节。它的字符集可以手动指定,也可以使用平台默认的字符集。

    构造方法:

    • public OutputStreamWriter(OutputStream out): 创建一个使用普通默认的字符集的字符流。

    • public OutputStreamWriter(OutputStream out,String charseName):创建一个指定的字符集字符流。

    练习:转换文件的编码

    将GBK编码的文本文件,转换为UTF-8编码的文件。

    分析:

    1. 指定GBK编码的转换流,读取文本文件。InputStreamReader
    2. 使用UTF-8编码的转换流,写入到文本文件中。OutputStreamWriter

    代码示例:

     public static void main(String[] args) throws IOException {
          //1.1 使用指定GBK编码的转换流,读取文本文件。InputStreamReader
         final InputStreamReader isr = new InputStreamReader(new FileInputStream("day30_IO\GBK.txt"), "GBK");        //1.2 使用UTF-8编码的转换流,写入到文本文件中。OutputStreamWriter 此时不写默认就是UTF-8
         final OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("day30_IO\UTF-8.txt"));
            //2. 使用转换流读取数据源文件
            //2.1 定义一个指定长度的字符数组
            char[] chars = new char[1024];
            //2.2 定义一个变量,记录读取到的有效字符个数
            int len = 0;
            //2.3 循环读取
            while ((len = isr.read(chars)) != -1) {
                // 读取出来的数据要写入到目的地文件中
                osw.write(chars, 0, len);
            }
            //3. 释放资源
            osw.close();
            isr.close();
        }
    
  • 相关阅读:
    hdu 1232 最小生成树
    hdu 1260 dp
    hdu 1385 最短路径按字典数输出
    hdu 1541 树状数组
    hdu 1544 求字符串回文
    hdu 1728
    hdu 1754 树状数组求最大值
    hdu 1892 二维树状数组
    hdu 2082 母函数
    循环
  • 原文地址:https://www.cnblogs.com/luayan/p/14143209.html
Copyright © 2011-2022 走看看