zoukankan      html  css  js  c++  java
  • day20---IO流概述

    20.01 IO流(IO流概述及分类)
     1.IO流用来处理设备之间的数据传输
      Java对数据的操作是通过流的方式
      Java用于操作流的类都在IO包中
     字节流:字节流可以操作任何数据,计算机中任何数据都以字节的形式存储的
     字符类:字符类只能操作纯字符数据,较为方便
    2.IO流常用父类
      字节流的抽象父类: InputStream OutputStram
      字符流的抽象父类: Reader Writer
    3.IO流程序书写
      使用前: 导入IO包中的类
      使用时: 进行IO异常处理
      使用后: 释放资源
    20.02 IO流(FileInputStream)
     1.read() 一次读入一个字节

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    public class day20_02 {
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            FileInputStream fis = new FileInputStream("xxx");    //创建流对象
            int x = fis.read();                    //从硬盘上读取一个字节,读取xxx文件内的 a
            int y = fis.read();                    //再次创建对象,指针向后移动一次,读取到xxx文件内的 b
            System.out.println(x);
            System.out.println(y);
            fis.close();                                //关流,释放资源
            System.out.println("-------------------");
            
            //优化版(一般使用如下方式)
            FileInputStream fis2 = new FileInputStream("xxx");
            int b ;
            while((b=fis2.read()) != -1){
                System.out.println(b);
            }
        }
    
    }
    View Code

    20.03 IO流(read()方法返回值为什么是int)
     1.read() 读取的是一个字节,为什么返回的是int,不是byte
      因为自己输入流可以操作任意类型的文件,比如音频图片等,这些文件底层都是二进制形式存储的,如果内次读取都返回byte
      有可能读到中间的时候遇到11111111,那么这11111111是byte类型的-1,这时候程序遇到-1就会停止
    20.04 IO流(FileOutputStream)
     2.write() 一次写出一个字节
    20.05 IO流(FileOutputStream追加)
      FileOutputStream的构造方法 写出数据 如何实现 数据的追加写入
      FileOutputStream fos = new FileOutputStream("yyy.txt",true);
    20.06 IO流(拷贝图片
     1.FileInputStream
     2.FileOutputStream

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    public class day20_06 {
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            FileInputStream  fis = new FileInputStream("02.jpg");    
            FileOutputStream fos = new FileOutputStream("copy.jpg");
            
            int b ;
            while((b=fis.read()) != -1){
                fos.write(b);
            }
            fis.close();
            fos.close();
        }
    }
    View Code

    20.07 IO流(拷贝音频文件画原理图)
    20.08 IO流(字节数组拷贝之available()方法)
      byte[] arr = new byte[fis.available()];//创建一个根输入文件一样大的数组
      此方法在开发中不推荐,加入要传一个10个G的电影,创建一个10个G的数组,不合理
    20.09 IO流(定义小数组)
    20.10 IO流(定义小数组的标准格式)

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    public class day20_09 {
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            FileInputStream  fis = new FileInputStream("xxx");    
            FileOutputStream fos = new FileOutputStream("copy2");
            
            //byte[] arr = new byte[2];
            byte[] arr = new byte[1024 * 8];                        //标准数组
            int len;
            while((len = fis.read(arr)) != -1)  {                    //第一次获取到文件上的a和b
                fos.write(arr,0,len);
            }
            fis.close();
            fos.close();
        }
    }
    View Code

    20.11 IO流(BufferedInputStream和BufferedOutputStream拷贝)
     1.缓冲思想
      字节流一次读写一个数组的速度明显比一次读写一个字节的速度快,这也是加入了缓冲区效果,
      Java本身在设计的时候就加入这样的思考,所以提供了字节缓冲区流
     2.BufferedInputStream
      BufferedInputStream内置了一个缓冲区(数组)
      从BufferedInputStream中读取一个字节时,BufferedInputStream会一次性读取8192个,存在缓冲区中返回给程序。
      程序再次读取时,就不用找文件了,直接从缓冲区中获取;直到所有的都被读取过了,才重新开始
     3.BufferedOutputStream
      BufferedOutputStream也内置了一个缓冲区(数组),
      程序向流中写出数据时,先写到缓冲区中,直到缓冲区写满,BufferedOutputStream才会把缓冲区的数据一次性写到文件里
     4.拷贝的代码

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    public class day20_10 {
        /**
         * @param args
         * @throws IOException 
         */
        public static <BufferInputStream> void main(String[] args) throws IOException {
            /*FileInputStream  fis = new FileInputStream("02.jpg");        //输入流对象,关联02.jpg
            FileOutputStream fos = new FileOutputStream("copy2.jpg");    //输出流对象,关联copy2.jpg
            
            BufferedInputStream  bis = new BufferedInputStream(fis);    //创建缓冲区对象,对输入流进行包装
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            */
            
            BufferedInputStream  bis = new BufferedInputStream( new FileInputStream("02.jpg"));    //创建缓冲区对象,对输入流进行包装
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy2.jpg"));
            
            int b;
            while((b = bis.read()) != -1){
                bos.write(b);
            }
            bis.close();
            bos.close();
        }
    }
    View Code

     5.小数组的读写和带有Buffer的哪个读写gengk
      定义小数组如果是8192个字节大小和Buffer比较的话
      定义小数组会略胜一筹,因为读和写是一个数组,Buffer操作的是两个数组
    20.12 IO流(flush和close方法的区别)
      flush() 刷新缓冲区的,刷新后可以再次写出
      close() 关闭释放流资源
    20.13 IO流(字节流读取中文)
     1.字节流读取中文:可能会读取到半个中文,造创乱码
     2.字节流写出中文:字节流直接操作的字节,所以写出中文必须将字符串转换为字节数组
    20.14 IO流(流的标准处理异常代码)
      try finally
     20.15 IO流(流的标准处理异常代码)
     20.16 IO流(图片加密)

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    public class day20_16 {
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            BufferedInputStream  bis = new BufferedInputStream( new FileInputStream("02.jpg"));    //创建缓冲区对象,对输入流进行包装
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy3.jpg"));
            
            /*运行此程序 生成照片打不开,  加密。关闭上面两行,打开下面两行  再次运行,解密
            BufferedInputStream  bis = new BufferedInputStream( new FileInputStream("copy3.jpg"));    //
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy4.jpg"));*/
            int b;
            while((b = bis.read()) != -1){
                bos.write(b^123);
            }
            bis.close();
            bos.close();
        }
    
    }
    加密

     20.17 IO流(拷贝文件)

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Scanner;
    public class day20_17 {
        /**
         * 分析:
         * 1.创建键盘录入对象
         * 2.定义方法对键盘录入的路径进行判断,是文件才返回
         * 3.在主方法中结束该文件
         * 4.读写该文件x
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            File file =  getFile();
            BufferedInputStream  bis = new BufferedInputStream( new FileInputStream(file));    //创建缓冲区对象,对输入流进行包装
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName()));
            int b;
            while((b = bis.read()) != -1){
                bos.write(b^123);
            }
            bis.close();
            bos.close();
        }
        /*定义一个方法获取键盘录入的路径,并封装成File
         * 1.返回值类型File
         * 2.参数列表无
         * */
        public static File getFile(){
            Scanner sc =new Scanner(System.in);            //创建键盘录入对象
            System.out.println("请输入一个文件的路径:");
            while(true){
                String line = sc.nextLine();            //接收键盘录入的路径
                File file = new File(line);
                if(!file.exists()){
                    System.out.println("录入的文件路径是:");
                }else if(file.isDirectory()){
                    System.out.println("您录入的是文件夹路劲,请重新录入:");
                }else{
                    return file;
                }
            }
        }
    }
    View Code

     20.18 IO流(录入数据拷贝到文件)

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Scanner;
    public class day20_18 {
        /**
         * 录入数据拷贝到文件
         * 分析:
         * 1.创建键盘录入对象
         * 2.创建输出流对象,关联text.txt
         * 3.定义无限循环
         * 4.遇到quit退出循环
         * 5.如果不quit,将内容写出
         * 6.关闭流
         *输出: 123
                123
                quit
    
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            Scanner sc =new Scanner(System.in);            //创建键盘录入对象
            FileOutputStream fos = new FileOutputStream("text.txt");
            System.out.println("请输入一个文件的路径:");
            while(true){
                String line = sc.nextLine();            //接收键盘录入的路径
                if("quit".equals(line)){
                    break;
                }
                fos.write(line.getBytes());                //字符串写出必须转换成字节数组
                fos.write("
    ".getBytes());
            }
            fos.close();
        }
    }
    View Code

     20.19 IO流(IO流概述及分类)
     20.20 IO流(IO流概述及分类)

  • 相关阅读:
    L2-1 功夫传人 (25分)
    7-11 家庭房产(25 分)
    7-11 玩转二叉树 (25分)
    7-10 排座位 (25分)
    7-12 最长对称子串 (25分)
    7-10 树的遍历 (25分)
    STL
    Max Gcd
    水果
    Rails
  • 原文地址:https://www.cnblogs.com/chengxiaofeng/p/10423588.html
Copyright © 2011-2022 走看看