IO streams in Java

Classification

  • According to the direction of the stream: input stream and output stream (www.1159880099.com) QQ1159880099
  • According to the size of the read text: byte stream and character stream
    (bytes are read according to bytes, reading Chinese is easy to garbled, and character streams are read according to characters, usually used to read Chinese)
  • Depending on how it is read: Node Streaming and Cache Streaming

(1) Byte stream

 【Basic Stream】

FileInputStream
FileInputStream fis= new FileInputStream("F:\\test.txt");    // You can pass in the File object

file read method

method one

1  /** 2  * Read the file byte by byte
 3  */ 
4      int n=0 ;
 5      StringBuffer sb= new StringBuffer();
 6      while ((n=fis.read())!=- 1 ){
 7          sb.append(( char )n);
 8      }
 9      System.out.println(sb);

 Method 2

1  /** 
2  * File < 1024 bytes, declare the byte array directly as the length of the input stream, read all text at once
 3   */ 
4      byte [] bytes= new  byte [fis.available()];
 5      StringBuffer sb= new StringBuffer();
 6      fis.read(bytes);
 7      sb.append( new String(bytes));
 8      System.out.println(sb);

 way three

1 /**
2  *文件>1024字节, 一次读取1024个字节
3  */
4     StringBuffer sb=new StringBuffer();
5     byte[] bytes=new byte[20];
6     int n=0;
7     while((n=fis.read(bytes))!=-1){
8            sb.append(new String(bytes));
9         }

FileOutputStream

  • FileOutputStream fos=new FileOutputStream("F:\\out.txt",true)
  • 如果第二个参数省略或,或传入false,则表示每次写入时将原文件清空,从文件头部开始写入
  • 如果第二个参数传入true,每次写入时,从原文件最后追加写入

 

1 StringBuffer sb=new StringBuffer();
2 fos.write(sb.toString().getBytes());
3 System.out.println(sb);

close

  • 输入流关闭:fis.close();
  • 输出流关闭:fos.close();

 


 【缓存流】

 BufferedInputStream_BufferedOutputStream

  • 在基本流的基础上进行包装,读取或写入文件时,将通过缓存进行即,先将内容写到缓存区中,缓存区满了以后,再进行读取或写入操作。
    可大大减少文件的操作次数,提高写入效率。称之为IO链。
    1 BufferedInputStream bis=null;
    2  bis=new BufferedInputStream(newFileInputStream("F:\\test.txt"));
    3 BufferedOutputStream bos=null;
    4 bos=new BufferedOutputStream(newFileOutputStream("F:\\out.txt",true));

     

  • IO链关闭时,我们只需关闭最外层流,内层流将自动关闭
    1 bis.close();

     

  • BufferedOutputStream在关闭前通常调用flush()方法,将缓存区剩余的内容刷新进行写入或读取操作。
    1 bos.flush();
    2 bos.close();

     



 【数据流】

DataInputStream_DataOutputStream

  • Data系列流,也有read和write方法,操作与基本流相同。
  • 采用二进制对文件进行读写操作,与基本流相比,可以直接读写Java中的基本数据类型。
     1 String name="zhangsan";
     2 int age=23;
     3 DataOutputStream dos=new DataOutputStream(new FileOutputStream("F:\\zhangsan.txt"));
     4 
     5 //对基本数据进行写入操作
     6 dos.writeUTF(name);
     7 dos.writeInt(age);
     8 
     9 DataInputStream dis=new DataInputStream(new FileInputStream("F:\\zhangsan.txt"));
    10 
    11 //对基本数据进行读取操作
    12 String uname=dis.readUTF();
    13 int uage=dis.readInt();

     

  • 如果操作的文件是一个二进制文件,需要使用DataOutputStream替代FileOutputStream。


【对象流】

ObjectInputStream_ObjectOutputStream

  • 与基本相同,可以直接使用read、write方法进行读写
  • .与Data流相同,可以对Java基本数据类型进行直接读写,readInt(),writeInt()
  • 可以只用readObject()和writeObject(),直接对对象进行操作。
    Person user=new Person("zhangsan",23,178.5,"山东烟台");
    ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("F:\\user.txt"));
     oos.writeObject(user);
    ObjectInputStream  ois=new ObjectInputStream(new FileInputStream("F:\\user.txt"));
     Person user1=(Person)ois.readObject();

     

【对象的序列化与反序列化】

  1. 对象的序列化:把对象的转换成字节序列永久地保存到硬盘上,通常存放在一个文件中;
  2. 对象的反序列化:字节序列恢复为Java对象的过程。
  • 如果:要将对象进行序列操作,那么实体类必须实现可序化接口 
    1 class Person implements Serializable{}

     

  • 当一个实体类实现可序化接口时,可以添加一个序列化版本号ID (警告提示,根据提示添加)
    1 private static final long serialVersionUID = 1L;

     


(二)字符流

【基本流】

FileWriter_FileReader(不常用)

  • 在处理数据单元时,以一个字符作为单位。
  • 在读写文件时,只能使用系统默认编码格式;无法指定编码,如果文件格式与系统默认格式不一致,
    那使用这两个方法读写将造成中文乱码。
  • 读写方法也是read(),write(),用法与字节流类似。



【字节流转字符流】

InputStreamReader_OutputStreamWriter

  • 将字节流转为字符流,同时支持自定义读写的编码格式;
    InputStreamReader isr=null;
    isr=new InputStreamReader(new FileInputStream("F:\\oo.txt"),"UTF-8");
      
    OutputStreamWriter  osw=null;
    osw=new OutputStreamWriter(new FileOutputStream("F:\\text.txt"),"UTF-8");

     

  • 常见编码格式:
    • ASCII:美国标准消息码‘
    • ISO08859-1:欧洲码
    • ANSI编码:
      • 简体中文:GB2312 GBK
      • 繁体中文:big-5
      • Unicode编码:国际标准码。兼容绝大部分国家的编码格式。
        UTF-6    UTF-8    UTF-16



【缓存流】

BufferedREader_BufferedWriter

  • 作用与用法与字节流相同
     1 BufferedReader br=null;
     2 
     3 br=new BufferedReader(
     4         new InputStreamReader(
     5             new FileInputStream("F:\\oo.txt"),"UTF-8"));
     6 int n=-1;
     7 while((n=br.read())!=-1){
     8         sb.append((char)n);
     9 }
    10 System.out.println(sb);
    11     
    12 BufferedWriter bw=null;         
    13 bw=new BufferedWriter(
    14         new OutputStreamWriter(
    15             new FileOutputStream("F:\\oo.html")));
    16 String s=sb.toString()+"jijn看到福克斯";

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324691791&siteId=291194637