• Java之IO流学习总结【下】


    2.字节流

     

    |-- InputStream(读)

    |-- OutputStream(写)

     

         由于字节是二进制数据,所以字节流可以操作任何类型的数据,值得注意的是字符流使用的是字符数组char[]而字节流使用的是字节数组byte[]。下面来看一个字节流读写文件的简单例子。

    清单7,使用字节流读写文本文件代码   

    private static void test5(){ 
    
        FileOutputStream fos=null; 
    
        try{ 
    
            fos=new FileOutputStream("D:/test.txt"); 
    
            fos.write(0010);//写入二进制数据 
    
            fos.flush(); 
    
        }catch(IOException e){ 
    
             
    
        }finally{ 
    
            try{ 
    
                fos.close(); 
    
            }catch(IOException ex){ 
    
                 
    
            } 
    
        } 
    
        FileInputStream fis=null; 
    
        try{ 
    
            fis=new FileInputStream("D:/test.txt"); 
    
            //fis.available()是获取关联文件的字节数,即test.txt的字节数 
    
            //这样创建的数组大小就和文件大小刚好相等 
    
            //这样做的缺点就是文件过大时,可能超出jvm的内存空间,从而造成内存溢出 
    
            byte[] buf=new byte[fis.available()]; 
    
            fis.read(buf); 
    
            System.out.println(new String(buf)); 
    
        }catch(IOException e){ 
    
             
    
        }finally{ 
    
            try{ 
    
                fos.close(); 
    
            }catch(IOException ex){ 
    
                 
            } 
    
        } 
    
    } 

    清单8,使用缓冲区对一张图片进行复制代码   

    private static void test6(){ 
    
        BufferedOutputStream bos=null; 
    
        BufferedInputStream bis=null; 
    
        try{ 
    
            //前面已经说过了,缓冲对象是根据具体的流对象创建的,所以必须要有流对象  
    
            bis=new BufferedInputStream(new FileInputStream("E:\images\wo\1.jpg")); 
    
            //写入目标地址 
    
            bos=new BufferedOutputStream(new FileOutputStream("E:\test.jpg")); 
    
            byte[] buf=new byte[1024]; 
    
            while((bis.read(buf))!=-1){ 
    
                bos.write(buf); 
    
            } 
    
            bos.flush(); 
    
        }catch(IOException e){ 
    
            e.toString(); 
    
        }finally{ 
    
            try{ 
    
                if(bos!=null){ 
    
                    bos.close(); 
    
                } 
    
                if(bis!=null){ 
    
                    bis.close(); 
    
                } 
    
            }catch(IOException ex){ 
    
                ex.toString(); 
    
            } 
    
        } 
    
    } 

    3.转换流

     

        特点

           |--是字节流和字符流之间的桥梁

           |--该流对象可以对读取到的字节数据进行指定编码表的编码转换

     

        何时使用

           |--当字节和字符之间有转换动作时

           |--流操作的数据需要进行编码表的指定时

     

        具体对象体现

           |--InputStreamReader:字节到字符的桥梁

           |--OutputStreamWriter:字符到字节的桥梁

     

        说明

          这两个流对象是字符流体系中的成员,它们有转换的作用,而本身又是字符流,所以在new的时候需要传入字节流对象。

     

        构造函数

           |--InputStreamReader(InputStream)

               通过该构造函数初始化,使用的是系统默认的编码表GBK。

           |--InputStreamReader(InputStream,String charset)

               通过该构造函数初始化,可以通过charset参数指定编码

           |--OutputStreamWriter(OutputStream)

               使用的是系统默认的编码表GBK。

           |--OutputStreamWriter(OutputSream,String charset)

               通过该构造函数初始化,可以通过参数charset指定编码。

     

       

           |--Reader

               |--InputStreamReader(转换流)

                  |--FileReader(文件字符流)

           |--Writer

               |--OutputStreamWriter(转换流)

                  |--FileWriter(文件字符流)

     

        说明

           转换流中的read方法,已经融入了编码表,在底层调用字节流的 read方法时将获取的一个或者多个字节数据进行临时存储,并去查指定的编码表,如果编码没有指定,则使用默认编码表。既然转换流已经完成了编码转换的动作,对于直接操作的文本文件的FileReader而言,就不用再重新定义了,只要继承该转换流,获取其方法,就可以直接操作文本文件中的字符数据了。

        注意

           在使用FileReader操作文本数据时,该对象使用的是默认的编码表, 如果要使用指定的编码表,必须使用转换流。

        代码体现

     

    FileReader fr = new FileReader(“test.txt”);
    
    InputStreamReader isr = new InputStreamReader(newFileInputStreamReader(“test.txt”));

         这两句代码意义相同,操作test.txt中的数据都是使用了系统默认的编码GBK。因为我们系统默认使用的编码表是GBK,如果test.txt中的数据是通过UTF-8形式编码的,那么在读取的时候就需要指定编码表,因此转换流必须使用InputStreamReader isr=new InputStreamReader(new FileInputStream(“a.txt”),”UTF-8”);

     

    四、流操作的基本规律

     

        |--明确数据源和数据汇(数据目的)

             其实是为了明确是输入流还是输出流

        |--明确操作的数据是否是纯文本数据

        |--说明

             数据源: 键盘System.in、硬盘、File开头的流对象、内存(数组)。

             数据汇: 控制台System.out、硬盘、File开头的流对象、内存(数组)。

        |--需求

             将键盘录入的数据存储到一个文件中和打印到控制台

        |--数据源System.in

             既然是源,使用的就是输入流,可用的体系有InputStream 、Reader。因为键盘录入进来的一定是纯文本数据,所以可以使用专门操作字符数据的Reader。而System.in对应 的流是字节读取流,所以要将其进行转换,将字节转换成字符即可,所以要使用Reader体系中的InputStreamReader,如果要提高效率,就使用BufferedReader,代码如:

         BufferedReader bur=new BufferedReader(new InputStreamReader(Sysem.in));

     

        |--数据汇:一个文件、硬盘

             数据汇一定是输出流,可以用的体系有OutputStream、 Writer。往文件中存储的都是文本数据,那么可以使用字符流较为方便Writer。因为操作的是一个文件,所以使用Writer 中的FileWriter,同理,要提高效率就要使用BufferedWriter。 代码如:

         BufferedWriter bufr=new BufferedWriter(new  FileWriter(“test.txt”));

     

     清单9,将键盘录入的数据存储到一个文件中和打印到控制台代码   

    private static void test7(){ 
    
        BufferedReader bur=null; 
    
        OutputStreamWriter osw=null; 
    
        BufferedWriter bw=null; 
    
        try{ 
    
            //数据源 
    
            bur=new BufferedReader(new InputStreamReader(System.in)); 
    
            //数据汇 
    
            osw=new OutputStreamWriter(System.out); 
    
            //数据汇,因为数据源用的是系统默认编码,所以这里可以直接使用FileWriter 
    
            //否则必须使用OutputStreamWriter转换流 
    
            bw=new BufferedWriter(new FileWriter("D:\test_target.txt")); 
    
            String line=null; 
    
            while((line=bur.readLine())!=null){ 
    
                osw.write(line); 
    
                osw.flush();//刷新到控制台 
    
                bw.write(line); 
    
                bw.flush();//刷新到文本文件 
    
            }    
    
        }catch(IOException e){ 
    
            e.toString(); 
    
        }finally{ 
    
            try{ 
    
                if(osw!=null){ 
    
                    osw.close(); 
    
                } 
    
                if(bur!=null){ 
    
                    bur.close(); 
    
                } 
    
                if(bw!=null){ 
    
                    bw.close(); 
    
                } 
    
            }catch(IOException ex){ 
    
                ex.toString(); 
    
            } 
        } 
    } 

     清单9是按照默认编码表写入文本文件的,那么如何按照指定编码表写入文件呢?其实也很简单,将清单9的代码稍微改一下就可以了。

     

    清单10代码   

    private static void test8(){ 
    
        BufferedReader bur=null; 
    
        BufferedWriter bw=null; 
    
        try{ 
    
            //数据源 
    
            bur=new BufferedReader(new InputStreamReader(System.in)); 
    
            //数据汇,按照指定编码格式存储到文本文件 
    
            bw=new BufferedWriter(new OutputStreamWriter(new  
    
            FileOutputStream("D:\test_target.txt"),"UTF-8")); 
    
            String line=null; 
    
            while((line=bur.readLine())!=null){ 
    
                bw.write(line); 
    
                bw.flush();//刷新到文本文件 
    
            }    
    
        }catch(IOException e){ 
    
            e.toString(); 
    
        }finally{ 
    
            try{ 
    
                if(bur!=null){ 
    
                    bur.close(); 
    
                } 
    
                if(bw!=null){ 
    
                    bw.close(); 
    
                } 
    
            }catch(IOException ex){ 
    
                ex.toString(); 
    
            } 
    
        } 
    
    } 
    
     

    既然写入文件时指定了编码,那么在读取的时候就必须指定该编码才能正确显示。

    清单11,读取指定编码表的文件代码   

    private static void test9() { 
    
        BufferedReader bur = null; 
    
        try { 
    
            // 注意,这里读取的是清单8写入的文件, 
    
            // 清单10用UTF-8编码格式写入, 
    
            // 所以在构造InputStreamReader时必须指定UTF-8编码 
    
            bur = new BufferedReader(new InputStreamReader( 
    
    new FileInputStream("D:\test_target.txt"), "UTF-8")); 
    
            String line = null; 
    
            while ((line = bur.readLine()) != null) { 
    
                System.out.println(line); 
    
            } 
    
        } catch (IOException e) { 
    
            e.toString(); 
    
        } finally { 
    
            try { 
    
                if (bur != null) { 
    
                    bur.close(); 
    
                } 
    
            } catch (IOException ex) { 
    
                ex.toString(); 
    
            } 
    
        } 
    
    } 

    写入和读取都做了,现在还差个复制操作,其实复制文件也很简单,先读取文件,再将读取到的数据写入文件,不同的是,在读取和写入时我们可以指定编码表。

     

    清单12代码   

    private static void test11() { 
    
        BufferedReader bur = null; 
    
        BufferedWriter buw = null; 
    
        try { 
    
            bur = new BufferedReader(new InputStreamReader( 
    
    new FileInputStream("D:\test_target.txt"), "UTF-8")); 
    
            buw = new BufferedWriter(new OutputStreamWriter( 
    
            new FileOutputStream("D:\test_target1.txt"),"UTF-8")); 
    
            String line = null; 
    
            while ((line = bur.readLine()) != null) { 
    
                buw.write(line); 
    
                buw.flush();// 刷新到文本文件 
    
            } 
    
        } catch (IOException e) { 
    
            e.toString(); 
    
        } finally { 
    
            try { 
    
                if (buw != null) { 
    
                    buw.close(); 
    
                } 
    
                if (bur != null) { 
    
                    bur.close(); 
    
                } 
    
            } catch (IOException ex) { 
    
                ex.toString(); 
    
            } 
    
        } 
    } 
  • 相关阅读:
    JS
    JS
    JS
    VUE
    element-ui 进入页面 message 自动触发的问题
    JS-数组中常用的方法
    CSS-transition简单过渡动画
    vue
    JS
    2021要买的书籍
  • 原文地址:https://www.cnblogs.com/onlysun/p/4561789.html
Copyright © 2020-2023  润新知