I / O streams - character stream and byte stream

A byte stream
  1, the output byte stream data file write to the specified OutputStream
  common methods:
    Close (): release resources
    flush (): Flush the stream, and forces all of the output bytes to write buffered
    write (byte [] b ): the specified byte array to the output stream
    write (byte [] b, int off, int len): the designated portion of the specified byte array to the output stream
    write (int b): the specified character written to the output stream
  Note OutputStream is the interface, for example, to use his implementation class FileOutputStream


  FileOutputStream constructor:
    FileOutputStream fos = newFileOutputStream (Stirng path);
    FileOutputStream fos = newFileOutputStream (File File);
      writing, writing is not false by default, by writing false
    FileOutputStream fos = newFileOutputStream (Stirng path, the append Boolean);
    FileOutputStream fos newFileOutputStream = (file file, the append Boolean);
  2, a data input stream of bytes read from the specified file IntputStream
    common methods:
      read (): read a byte
      read (byte [] b): read a specified byte read array size
    Note: IntputStream is an interface, for example, with his FileIntputStream implementation class

using a stream of bytes to copy files:
    

 1 // 明确数据源
 2         File f2 = new File("D:\\demo0723\\lifecycle.png");
 3         FileInputStream fis = new FileInputStream(f2);
 4         // 目的地
 5         FileOutputStream fos = new FileOutputStream("D:\\demo0723\\a\\lifecycle(2).png");
 6         byte[] b = new byte[1024];
 7         int len2 = 0;
 8         while ((len = fis.read(b)) != -1) {
 9             fos.write(b,0,len);
10         }
11         fis.close();
12         fos.close();

 


Second, the character stream
  1, input stream of bytes: Reader
    Reader is an interface, with his implementation class, e.g. FileReader
      common methods: read (): read a byte
           read (char [] char): read the specified char array length
  FileReader class:
    Constructors:
      FileReader fr = new new FileReader (File File);
      FileReader fr = new new FileReader (String path);
  2, output stream of bytes: the Write
    the Write is an interface, with his implementation class, e.g. FileWriter
      common methods:
        write (char [] char): write to the specified char array
        write (char [] char, int off, int len): write to the specified char array specifies the length of
        write (int c): a single character is written to the specified
        write (string str): writes the specified string
        write (string str, int off, int len): Specifies the length of the string is written
    FileWriter class:
      constructor:
        Fw = new new FileWrite FileWrite (File File);
        FileWrite fw = new new FileWrite (String path);
          writing does not default false writing, writing with false
        FileWrite fw = new new FileWrite (File File, boolean the append);
        FileWrite fw = new new FileWrite (String path, boolean the append);

  flush () and close () the difference:
    flush (): flush the stream, forcing the buffer of characters written to the file, but can continue to use
    close (): there flush () function, but after using the flow is no longer available

Copy the file byte stream

  

 1 // 使用字符流复制
 2         FileReader fr = new FileReader(f);
 3         FileWriter fw = new FileWriter("D:\\demo0723\\a\\lifecycle(1).png");
 4 
 5         char[] ch = new char[1024];
 6         int len = 0;
 7         while ((len = fr.read(ch)) != -1) {
 8             fw.write(ch, 0, len);
 9         }
10         fw.close();
11         fr.close();

How to distinguish between byte and character streams

  

    Byte stream: Stream ending with
    the character stream: A Reader and Writer ending

Guess you like

Origin www.cnblogs.com/yanghaoyu0624/p/11706946.html