OutputStream IO stream of Detailed

Write byte stream

OutputStream abstract class

  • This abstract class is a superclass of all classes of the output byte stream. Receiving the output byte stream and outputs the bytes transmitted to a receiver.
  • Common methods:

>void close() 
>           关闭此输出流并释放与此流有关的所有系统资源。 
> 
>void flush() 
>           刷新此输出流并强制写出所有缓冲的输出字节。 
> 
>void write(byte[] b) 
>           将 b.length 个字节从指定的 byte 数组写入此输出流。 
>           
>void write(byte[] b, int off, int len) 
>           将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。 
> 
> abstract  void write(int b) 
>           将指定的字节写入此输出流。

FileOutputStream子类

  • Directly inserted in the file, write the file data directly

Create an object:

  • FileOutputStream (String name)
    Creates a write data to a file with the specified name of the output file stream.
  • FileOutputStream (File file)
    to create a file for writing data file represented by the specified File object in the output stream.
  • FileOutputStream (File file, boolean append) - append
    to create a file is written to the data file represented by the specified File object in the output stream.

BufferedOutputStream子类

  • This class implements a buffered output stream. By providing such an output stream, the application can write the respective underlying output stream of bytes, the bytes are written instead of for each call to the underlying system.

Create Object

  • BufferedOutputStream(OutputStream out)
  • Create a new buffered output stream to write data to the specified underlying output stream.

Written character stream

Writer abstract classes: Abstract class for writing character streams.

Common methods:
  • void write (char [] cbuf)
    writes the character array.
  • abstract void write (char [] cbuf
    , int off, int len) Write a portion of an array of characters.
  • void write (int c)
    Write a single character.
  • void write (String str)
    write the string.
  • void write (String str, int off
    , int len) Write a portion of a string.
  • abstract void close ()
    Close the stream, flushing it first.

OutputStreamWriter子类

OutputStreamWriter, the bridge is a character streams to byte streams: using a specified charset character written to the stream into byte code. It uses the character set can be specified by name or explicitly given, otherwise it will accept the platform's default character set.

Create an object:

  • OutputStreamWriter (OutputStream out, String charsetName)
    created using the specified character set OutputStreamWriter.
  • OutputStreamWriter (OutputStream out)
    Creates a default character encoding OutputStreamWriter.
public class rr {
       public static void main(String[] args) throws Exception {
//           method1();//字节写出
              method2();//字符写出
       } 
       private static void method2() throws Exception {
              Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("D:\\\\teach\\\\a.txt"))                                                                                                                                                                                            , "utf-8"));
              long s = System.currentTimeMillis();
              for(int i = 48 ; i < 1000000; i++) {
                     out.write(i);
              }
              s = System.currentTimeMillis() - s;
              System.out.println(s + "--");//266               
              out.close();
       } 
       private static void method1() throws Exception {
              long s = System.currentTimeMillis();
              OutputStream out = new FileOutputStream(new File("D:\\teach\\a.txt"));
              for(int i = 48 ; i < 1000000; i++) {
                     out.write(i);
              }
              s = System.currentTimeMillis() - s;
              System.out.println(s + "--");//3484               
              long ss = System.currentTimeMillis();
              OutputStream out2 = new BufferedOutputStream(new FileOutputStream(new File("D:\\teach\\a2.txt")));
              for(int i = 48 ; i < 1000000; i++) {
                     out2.write(i);
              }
              ss = System.currentTimeMillis() - ss;
              System.out.println(ss + "==");//54               
              out.close();
              out2.close();
       }
}
Published 36 original articles · won praise 13 · views 1065

Guess you like

Origin blog.csdn.net/weixin_44598691/article/details/104775408
Recommended