zoukankan      html  css  js  c++  java
  • java基础-IO流

    IO原理及流的分类:
    文件流:FileInputStream / FileOutputStream / FileReader / FileWriter    (除了这四个是节点流,其他都是处理流)
    缓冲流:BufferedInputStream / BufferedOutputStream / BufferedReader / BufferedWriter
    转换流:InputStreamReader / OutputStreamWriter
    标准输入/输出流:System.in / System.out
    打印流:PrintStream / PrintWriter
    数据流:DataInputStream / DataOutputStream
    对象流:ObjectInputStream / ObjectOutputStream
    随机存取文件流:RandomAccessFile

    1. File对象不仅可以表示一个文件,也可以表示一个文件目录

         文件可以删除,创建,重命名等等表面信息(不可以修改内容,修改由IO来完成),文件目录可以创建目录,获取文件目录下的所有文件。

    2. 流分类:如果是尚硅谷课程的示意图

        按操作数据单位不同分为:字节流(8 bit)(图片,音频,视频等),字符流(16 bit) (文本文件)
        按数据流的流向不同分为:输入流,输出流
        按流的角色的不同分为:节点流(直接作用于文件),处理流(包含着节点流或者处理流,间接)

     3.FileIputStream,FileOutStream,Reader,Writer

    伪代码:
        //内容为abc的txt文件,读取并创建对象
        File file = new File("D:/hello.txt");
        //创建文件输出字节流(字符不可读取)
        FileInputStream fis = new FileInputStream(fis);
        //read()方法
        int b =0 ;
        //读取一个字节,并返回给b,返回的b是txt文件字节的ascii码
        while((b=fis.read())!=-1){//如果读到结尾,没有字节了,就会返回-1
              System.out.print(b);
        }
        
        //read(byte[] byte)方法
        int len;//每次写入byte缓冲区的字节总数
        byte[] by = new byte[20];//读取到这个数组中
        while((len=fis.read(by))!=-1){//如果读到结尾,没有字节了,就会返回-1
              System.out.print(len);//如果有22个字节,输出202
        }
    
    复制文件伪代码:
        File filein = new File("in.txt");
        File fileout = new File("out.txt");
        FileInputStream fis = new FileInputStream(filein);
        FileOutputStream fos = new FileOutputStream(fileout);
        byte[] b = new byte[20];
        int len;
        while((b = fis.read(b)) != -1){
            fos.write(b,0,len);
            //不可以写成:fos.write(b);
        }
        fos.close();
        fis.close();
    

     

    4. 缓冲流:BufferedInputStream,BufferedOutputStream,BufferedReader,BufferedWriter    (加一层缓冲流,让速度更快)

    缓冲流实现文件复制伪代码:
    public void copy(){
    
        //1.读入写出文件
        File filein = new File("in.txt");
        File fileout = new File("out.txt");
        //2. 创建相应的流
        //先创建节点流
        FileInputStream fis = new FileInputStream(filein);
        FileOutputStream fos = new FileOutputStream(fileout);
        //将创建的节点流传给缓冲流
        BufferedInputStream bis = new BufferedInputStream(fis); 
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //3.实现复制
        byte[] b = new byte[1024];
        int len;
        while((len = bis.read(b)) != -1){
            bos.write(b , 0 , len);
        }   
         //4.关闭流
        bos.close();
        bis.close();
    }
    

      缓冲流区别:1.字节流每一次读取都是一次IO操作,频繁的IO操作让整个复制过程效率变低;

                                2.给字节流套上一个缓冲流,相当于每次读取或者写出都将内容放到了一个Buffered缓冲区中,缓冲区满了一次性读取或者写入,完成一次IO操作,效率要高。

     5.转换流:InputStreamReader,OutputStreamWriter

        注意:InputStreamReader是输入字节流转换成字符流  

                  OutputStreamWriter是输出字符流转换成字节流  

    public void tranfer(){
    
        //InputStreamReader  OutputStreamWriter
        File file = new File("a.txt");
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fis,"GBK");
        BufferedReader br = new BufferedReader(isr);
        //至此完成了字节流到字符流的转换
        
        File file1 = new File("b.txt");
        FileOutputStream fos = new FileOutputStream(file1);
        OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
        BufferedWriter bw = new BufferedWriter(osw);
    }
    

    6.标准输入输出流System.in  System.out

    public void standinout(){
    
        InputStream is = System.in;
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String str;
        while(true){
            System.out.println("请输入:");
            str = br.readLine();
            if(str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")){
                 break;
            }
            //转换成大写
            System.out.println(str.toUpperCase());
        }
    }
    

    7. 打印流

    8. 数据流(操作java的基本数据类型)

    9. 对象流(操作java的对象)

        对象序列化机制:允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。当其它程序获取了这种二进制流,就可以恢复成原来的Java对象。

        优点:可将任何实现了Serializable接口的对象转化为字节数据,使其在保存和传输时可被还原。

    10. 随机存取文件流 RandomAccessFile(可以读取文件的任意一部分,尾部添加内容,但是却不用占太多内存,对于大文件添加内容来说相当实用)

    public void addtxt(){  
            //创建对象
            RandomAccessFile raf=new RandomAccessFile("a.txt", "rw");         
            //将记录指针移动到文件最后  
            raf.seek(raf.length());  
            //追加内容
            raf.write("suffix".getBytes());  
    }  
    

      

  • 相关阅读:
    拷贝构造,移动构造,右值引用,左值,右值,std::move,std::forward,std::ref
    枚举类型 enum以及enum class
    C++ 静态库LIB的使用方法
    array(数组容器)
    C++标准模板库STL
    C++ 动态库DLL的使用方法
    函数指针与回调函数
    VS项目属性等一系列问题
    逻辑运算符(且或非),位运算符(异或),函数对象运算(bit_or)
    pinpoint-grpc编译异常问题记录
  • 原文地址:https://www.cnblogs.com/winv758241/p/7409998.html
Copyright © 2011-2022 走看看