ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

IO流

2022-08-08 21:34:09  阅读:403  来源: 互联网

标签:Scanner BufferedReader FileOutputStream IO FileInputStream new 序列化


FileInputStream:

  用于文件读取数据,可以用new创建输入流对象

    InputStream  Input = new FileInputStream();                --InputStream是抽象类,无法实例化,用FileInputStream接收

    byte[] data = new byte[1024];    

 

FileOutputStream:

  向文件中写入数据,如果文件不存在则会创建

    有两个构造方法可以用来创建FileOutputStream对象

    ①OutputStream  f = new  FileOutputStream("D:/java/h");

    ②File f = new File("D:/java/c");

      OutputStream fout = new FileOutPutStream(f);

    如果文件已存在则会覆盖原文件内容, 如果不想覆盖原文件内容,将(f)改为(f.true)   //默认值为f.false

Reader:

  包装FileInputStream流

    Reader reader = new InputStreamReader(xx);

    关闭流时先打开的后关,后打开的先关

 

    乱码问题:①将文件编码设置为ANSI    ②在reader时手动设置为utf-8  -- reader = new InputStreamReader(f,"utf-8")

   -----------------FileReader 相当于Reader   但他不用包装FileInputStream,写法更简单,但是他不能修改字符编码,只能为ANSI(无法进行字符操作)

 

序列化:      

          该类必须实现java.io.Serialization接口

               序列化反序列化:对对象的读写。

          序列化:对象  --->   txt文件   

          反序列化:txt文件   ----->    对象

  

              OutputStream ops = new FileOutputStream();        -------------序列化   

              ObjectOutputStream  oop  =  new  ObjectOutputStream(ops)     -------------序列化        

                  oop.writeObject(stu);

          

              InputStream  ins  =  new  FileInputStream();                               ------------反序列化

              ObjectInputStream  ois  =  new  ObjectInputStream(ins);            ------------反序列化

                    Object  obj  =  ois.readObject();

 

               if(obj  instanceof  Student){

                Student  stu1 = (Student) obj;

                System.out.println(stu1.getName  +  stu1.getAge);

                }

 

 

二进制读取

        读:                                         FileInputStream    fis  =  new FileInputStream(file);

                  DataInputStream  dis  =  new  DataInputStream(fis);

                    byte[]  data  =  new  byte[fis.available()];

                    dis.read(data)                        fis.available这个方法可以在读写操作前先得知数据流里有多少个字节可以读取

             

      写:                  FileOutputStream  fos  =  new  FileOutputStream(file);

                   DataOutputStream  dos  =  new  DataOutputStream(fos);

                           dos.write(data);

 

 

 

 

 

 

 

 

 

    BufferedReader 是支持同步的,而 Scanner 不支持。如果我们处理多线程程序,BufferedReader 应当使用。

    BufferedReader 相对于 Scanner 有足够大的缓冲区内存。

    Scanner 有很少的缓冲区(1KB 字符缓冲)相对于 BufferedReader(8KB字节缓冲),但是这是绰绰有余的。

    BufferedReader 相对于 Scanner 来说要快一点,因为 Scanner 对输入数据进行类解析,而 BufferedReader 只是简单地读取字符序列。

标签:Scanner,BufferedReader,FileOutputStream,IO,FileInputStream,new,序列化
来源: https://www.cnblogs.com/On1on/p/16563498.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有