zoukankan      html  css  js  c++  java
  • 字节流、字符流、缓冲流

    字节流

    字节输出流OutputStream(抽象类)

    常用方法:

    实例:

    public static void main(String[] args) throws IOException {
            //FileOutputStream的构造方法:如果文件存在,则覆盖,如果不存在,则创建
            // 
      换行
            FileOutputStream fos=new FileOutputStream("x:\test\bian.txt",true);
            /*fos.write(49);
            fos.write(48);
            fos.write(48);*/
            /*byte[] bytes={-65,-66,-67,-68};
            fos.write(bytes,1,2);*/
            fos.write("xuezhiqian
    ".getBytes()); //字符串转字节
            /*new String(bytes);*/    //字节转字符串
            fos.close();
        }

    FileOutputStream类(文件输出流,OutputStream的子类)

    FileOutputStream类写入数据到文件中

    代码实现:
    public static void main(String[] args){
        FileOutputStream fos=null;
            try {
                fos = new FileOutputStream("x:\test\bb.txt");
                fos.write(12);
            } catch (IOException e) {
                throw new RuntimeException("输入失败");
            }finally{
                try {
                    if(fos!=null)
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException("关闭失败");
                }
            }

     给文件中续写和换行

    构造方法:

    代码实现:

    public static void main(String[] args) throws IOException {
            //FileOutputStream的构造方法:如果文件存在,则覆盖,如果不存在,则创建
            // 
      换行
            FileOutputStream fos=new FileOutputStream("x:\test\bian.txt",true);
            /*fos.write(49);
            fos.write(48);
            fos.write(48);*/
            /*byte[] bytes={-65,-66,-67,-68};
            fos.write(bytes,1,2);*/
            fos.write("xuezhiqian
    ".getBytes()); //字符串转字节
            /*new String(bytes);*/    //字节转字符串
            fos.close();
        }

    IO异常的处理

    代码实现:

    public class FileOutputStreamDemo3 {
        public static void main(String[] args) {
            File file = new File("c:\file.txt");
            //定义FileOutputStream的引用
            FileOutputStream fos = null;
            try {
                //创建FileOutputStream对象
                fos = new FileOutputStream(file);
                //写出数据
                fos.write("abcde".getBytes());
            } catch (IOException e) {
                System.out.println(e.toString() + "----");
    throw new RuntimeException("文件写入失败,重试");
    
            } finally {
                //一定要判断fos是否为null,只有不为null时,才可以关闭资源
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        throw new RuntimeException("关闭资源失败");
                    }
                }
            }
        }
    }

         字节输入流InputStream

    常用方法:

    FileInputStream类

     

    构造方法:

    FileInputStream类读取数据read方法

    代码实现:

    public static void main(String[] args) throws IOException {
            FileInputStream fis=new FileInputStream("x:\test\test.txt");
            int len=0;
            while((len=fis.read())!=-1){
                System.out.println((char)len);
            }
            /*int a=fis.read();
            System.out.println((char)a);
            int b=fis.read();
            System.out.println((char)b);
            int c=fis.read();
            System.out.println((char)c);
            int d=fis.read();
            System.out.println((char)d);
            int e=fis.read();
            System.out.println((char)e);
            int f=fis.read();
            System.out.println((char)f);*/
            /*char ch=97;
            System.out.println(ch);*/
            fis.close();
        }
    
    }

     读取数据read(byte[])方法

    代码实现:

    public class InputStreamDemo2 {
        public static void main(String[] args) throws IOException {
            FileInputStream fis=new FileInputStream("x:\test\test.txt");
            byte[] bytes=new byte[2];
            int len=0;
            while((len=fis.read(bytes))!=-1){
                System.out.println(new String(bytes,0,len));
            }
        /*    //第一次读
            int len=fis.read(bytes);
            System.out.println(len);
            System.out.println(new String(bytes));
            //第二次读
            len=fis.read(bytes);
            System.out.println(len);
            System.out.println(new String(bytes));
            //第三次读
            len=fis.read(bytes);
            System.out.println(len);
            System.out.println(new String(bytes));
            //第4次读
            len=fis.read(bytes);
            System.out.println(len);
            System.out.println(new String(bytes));*/
            fis.close();
        }
    
    }

    字节流复制文件

    代码实现:

    public class CopyDemo {
        public static void main(String[] args) throws IOException {
            long time1=System.currentTimeMillis();
            //1.明确数据源(要复制哪个文件)
            FileInputStream fis=new FileInputStream("C:\Program Files\java\eclipse.zip");
            //2.明确目的地(要复制到哪个文件)
            FileOutputStream fos=new FileOutputStream("x:\test\d\java.txt");
            //3.复制
            int len=0;
            while((len=fis.read())!=-1){
                fos.write(len);
            }
            long time2=System.currentTimeMillis();
            System.out.println(time2-time1);
            //4.释放资源
            fis.close();
            fos.close();
        }
    
    }

    缓冲数组方式复制文件

    代码实现:

    public static void main(String[] args) throws IOException {
            long time1=System.currentTimeMillis();
            // 1.明确数据源
            FileInputStream fis=new FileInputStream("C:\Program Files\java\eclipse.zip");
            //2.明确目的地
            FileOutputStream fos=new FileOutputStream("x:\test\eclipse.zip");
            //3.复制
            int len=0;//获取读取字节的有效长度
            byte[] bytes=new byte[1024];
            while((len=fis.read(bytes))!=-1){
                fos.write(bytes, 0, len);
            }
            long time2=System.currentTimeMillis();
            System.out.println(time2-time1);
            //4.释放资源
            fis.close();
            fos.close();
        }
    }

     字符流

     字符输入流Reader

    基础方法:

    代码实现:

    public class ReaderDemo {
    
        public static void main(String[] args) throws IOException {
            //通过字符数组的方式读
            FileReader fr=new FileReader("x:\test\test.txt");
            int len=0;
            char[] ch=new char[1024];
            while((len=fr.read(ch))!=-1){
                System.out.println(new String(ch,0,len));
            }
            fr.close();
            //method1(fr);
        }
        public static void method1(FileReader fr) throws IOException{
            //通过一个字符一个字符的方法读
            int len=0;
            while((len=fr.read())!=-1){
                System.out.print((char)len);
            }
            fr.close();
        }
    
    }

      FileReader类

    构造方法:

    使用FileReader读取包含中文的文件

    代码实现:

    public class CharStreamDemo {
        public static void main(String[] args) throws IOException {
            //给文件中写中文
            writeCNText();
            //读取文件中的中文
            readCNText();
        }    
        //读取中文
        public static void readCNText() throws IOException {
            FileReader fr = new FileReader("D:\test\cn.txt");
            int ch = 0;
            while((ch = fr.read())!=-1){
                //输出的字符对应的编码值
                System.out.println(ch);
                //输出字符本身
                System.out.println((char)ch);
            }
        }
        //写中文
        public static void writeCNText() throws IOException {
            FileOutputStream fos = new FileOutputStream("D:\test\cn.txt");
            fos.write("欢迎你".getBytes());
            fos.close();
        }
    }

          字符输出流Writer

    基础方法:

     

        FileWriter类

     构造方法:

    代码实现:

    public class WriterDemo {
        public static void main(String[] args) throws IOException {
          //写入中文字符 
         FileWriter fw=new FileWriter("x:\test\test.txt"); 
         fw.write("段子手薛之谦");//被写在缓冲区 
         fw.flush();//可刷新多次 
         //fw.close();//具有一次刷新功能 
         }
     }

    flush()和close()的区别

    close是在关闭之前刷新一次

    flush是一边存一边刷

    字符流复制文件

    代码实现:

    public class JpgCopy1 {
        //字符流复制文件
        public static void main(String[] args) throws IOException {
            //1.明确数据源
            FileReader fr=new FileReader("X:\test\img1.jpg");
            //2.明确目的地
            FileWriter fw=new FileWriter("x:\test\a\img1.jpg");
            //3.复制
            int len=0;
            char[] ch=new char[1024];
            while((len=fr.read(ch))!=-1){
                fw.write(ch, 0, len);
            }
            //4.释放资源
            fr.close();
            fw.close();
        }
    
    }

    转换流

     OutputStreamWriter类(OutputStreamWriter 是字符流通向字节流的桥梁)

    代码实现:

    public class OutputStreamWriterDemo {
        public static void main(String[] args) throws IOException {
            FileOutputStream fos=new FileOutputStream("x:\test\utf8.txt",true);
            OutputStreamWriter osw=new OutputStreamWriter(fos,"utf-8");
            osw.write("这是utf-8编码");
            osw.close();
        }
    }

      InputStreamReader类(InputStreamReader 是字节流通向字符流的桥梁)

    代码实现:

    public class InputStreamReaderDemo {
    
        public static void main(String[] args) throws IOException {
            FileInputStream fis=new FileInputStream("x:\test\utf8.txt");
            InputStreamReader isr=new InputStreamReader(fis,"utf-8");
            char[] ch=new char[1024];
            int len=0;
            while((len=isr.read(ch))!=-1){
                System.out.println(new String(ch,1,len-1));
            }
            isr.close();
        }
    
    }

        转换流和子类区别

    OutputStreamWriter:

                |--FileWriter:

    InputStreamReader:

       |--FileReader;

    OutputStreamWriter和InputStreamReader是字符和字节的桥梁:也可以称之为字符转换流。字符转换流原理:字节流+编码表。

    FileWriter和FileReader:作为子类,仅作为操作字符文件的便捷类存在。当操作的字符文件,使用的是默认编码表时可以不用父类,

    而直接用子类就完成操作了,简化了代码。

    InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));//默认字符集。
    
     
    
    InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"GBK");//指定GBK字符集。
    
     
    
    FileReader fr = new FileReader("a.txt");

    这三句代码的功能是一样的,其中第三句最为便捷。

    注意:一旦要指定其他编码时,绝对不能用子类,必须使用字符转换流。什么时候用子类呢?

    条件:

    1、操作的是文件。2、使用默认编码。

    总结:

    字节--->字符 : 看不懂的--->看的懂的。  需要读。输入流。 InputStreamReader

    字符--->字节 : 看的懂的--->看不懂的。  需要写。输出流。 OutputStreamWriter

       缓冲流

          字节缓冲流

     字节缓冲输出流BufferedOutputStream

     字节缓冲输入流 BufferedInputStream

     使用基本的流与高效的流完成复制文件

     }

      字符缓冲流

    {

        字符缓冲输出流 BufferedWriter

        字符缓冲输入流 BufferedReader

        使用字符缓冲流完成文本文件的复制

    }

    整体代码实现:

    public class Demo01 {
        public static void main(String[] args) throws IOException {
            //method1();
            //method2();
            copy();
        }
        //缓冲字符输出流
        //bw.newLine();可以实现平台无关性
        //win 换行 
    
        //Linux 换行 
    
        public static void method1() throws IOException{
            FileWriter fw=new FileWriter("x:\test\test.txt",true);
            BufferedWriter bw=new BufferedWriter(fw);
            bw.write("你好吗".toCharArray());
            bw.flush();
            bw.newLine();//换行
            bw.write("我不好");
            bw.flush();
            bw.newLine();
            bw.write(100);
            bw.close();
        }
        //缓冲字符输入流
        public static void method2() throws IOException{
            FileReader fr=new FileReader("x:\test\test.txt");
            BufferedReader bfr=new BufferedReader(fr);
            String line=null;
            int count=0;
            while((line=bfr.readLine())!=null){
                count++;
                System.out.println(count+"    "+line);
            }
            /*System.out.println(bfr.readLine());
            System.out.println(bfr.readLine());
            System.out.println(bfr.readLine());
            System.out.println(bfr.readLine());*/
            
            bfr.close();
        }
        //缓冲字符流的复制
        public static void copy() throws IOException{
            //1.明确数据源
            FileReader fr=new FileReader("x:\test\test.txt");
            BufferedReader br=new BufferedReader(fr);
            //2.确定目的地
            FileWriter fw=new FileWriter("x:\test\d\copy.txt");
            BufferedWriter bw=new BufferedWriter(fw);
            //3.复制
            String line=null;
            while((line=br.readLine())!=null){
                bw.write(line);
                bw.newLine();
                bw.flush();
                
            }
            br.close();
            bw.close();
        }
    
    }
    public class Demo02 {
        public static void main(String[] args) throws IOException {
             //method1();
             method2();
        }
        //缓冲字节输出流
        public static void method1() throws IOException{
            FileOutputStream fos=new FileOutputStream("x:\test\test.txt",true);
            BufferedOutputStream bos=new BufferedOutputStream(fos);
            //bos.write(100);
            bos.write("你好啊".getBytes());
            bos.close();
        }
        //缓冲字节输入流
        public static void method2() throws IOException{
            FileInputStream fis=new FileInputStream("x:\test\test.txt");
            BufferedInputStream bis=new BufferedInputStream(fis);
            int len=0;
            while((len=bis.read())!=-1){
                System.out.print((char)len);
            }
            bis.close();
        }
    
    }

    整体练习:

    public class Demo01 {
        public static void main(String[] args) throws IOException {
            method1();//31159
            method2();//52
            method3();//105
            method4();//3
        }
        public static void method1() throws IOException {
            //字节流read() 
            long time1=System.currentTimeMillis();
            FileInputStream fis=new FileInputStream("x:\test\mysql-connector-java-5.1.37-bin.jar");
            FileOutputStream fos=new FileOutputStream("x:\test\a\mysql-connector-java-5.1.37-bin.jar");
            int len=0;
            while((len=fis.read())!=-1){
                fos.write(len);
            }
            long time2=System.currentTimeMillis();
            System.out.println(time2-time1);
            fis.close();
            fos.close();
        }
        public static void method2() throws IOException{
            //字节流read(byte[])
            long time1=System.currentTimeMillis();
            FileInputStream fis=new FileInputStream("x:\test\mysql-connector-java-5.1.37-bin.jar");
            BufferedInputStream bis=new BufferedInputStream(fis);
            FileOutputStream fos=new FileOutputStream("x:\test\kkk.jar");
            BufferedOutputStream bos=new BufferedOutputStream(fos);
            int len=0;
            byte[] bytes=new byte[1024];
            while((len=fis.read(bytes))!=-1){
                fos.write(bytes, 0, len);
            }
            long time2=System.currentTimeMillis();
            System.out.println(time2-time1);
            fis.close();
            fos.close();
        }
        //缓冲字节输出流
            public static void method3() throws IOException{
            
                long time1=System.currentTimeMillis();
                FileInputStream fis=new FileInputStream("x:\test\mysql-connector-java-5.1.37-bin.jar");
                BufferedInputStream bis=new BufferedInputStream(fis);
                FileOutputStream fos=new FileOutputStream("x:\test\ggg.jar");
                BufferedOutputStream bos=new BufferedOutputStream(fos);
                int len=0;
                while((len=bis.read())!=-1){
                    bos.write(len);
                }
                long time2=System.currentTimeMillis();
                System.out.println(time2-time1);
                bis.close();
                bos.close();
            }
            //缓冲字节输入流
            public static void method4() throws IOException{
                long time1=System.currentTimeMillis();
                FileInputStream fis=new FileInputStream("x:\test\mysql-connector-java-5.1.37-bin.jar");
                BufferedInputStream bis=new BufferedInputStream(fis);
                FileOutputStream fos=new FileOutputStream("x:\test\mysql-connector-java-5.1.37-bin.jar");
                BufferedOutputStream bos=new BufferedOutputStream(fos);
                int len=0;
                byte[] bytes=new byte[1024];
                while((len=bis.read(bytes))!=-1){
                    bos.write(bytes, 0, len);
                }
                long time2=System.currentTimeMillis();
                System.out.println(time2-time1);
                bis.close();
                bos.close();
            }
        
        }
  • 相关阅读:
    Beetle在TCP通讯中使用协议分析器和自定义协议对象
    Beetle在TCP通讯中使用AMF3协议和Flash通讯
    发布一个TCP 吞吐性能测试小工具
    替书仙澄清一些东西,并且对无知的人谈谈网络追踪
    2006年4月1日测彩研究
    构建工具研究:该死的Maven的j2me构建
    2006年4月2日测彩研究
    Eclipse的插件代码折叠
    JAVA这堆IDE........无言
    假如人生不曾相遇(转)
  • 原文地址:https://www.cnblogs.com/qq1312583369/p/10248474.html
Copyright © 2011-2022 走看看