Common input and output streams in Java

Java input and output streams in accordance with the format can be divided into a byte stream and a stream of characters :( pairs)

    字节流:FileInputStream,FileOutputStream   ;  BufferedInputStream,BufferedOutputStream  ;

    字符流:FileReader,FileWriter ; BufferedReader,BufferedWriter

The main difference between character streams and byte streams:

            1. When the byte read from the stream, to read a byte returns a byte; (English character is one byte)

              Character stream using a byte stream read one or more bytes (a byte in Chinese two, three bytes in UTF-8 code table, which depends on the coding scheme, different coding different in Chinese size) time. Go check the specified encoding table, found the character to return.

            2. byte stream can handle all types of data, such as: images, MP3, AVI video files, and the character stream can only deal with character data. As long as the plain text data, necessary to give priority to a character stream, are used in addition to byte stream .

 

The following byte stream and character stream sequentially described:

   Byte stream:

     1. FileInputStream, FileOutputStream (both of which are paired, the former for the input (file read) operation, which is carried out (write file) Operation )

            It referred to the FileInputStream file input stream. Its role is to input data file into memory, we can use it to read the file. Because it is a stream of bytes, so the problem may occur when reading Unicode characters (such as Chinese) files. The class constructor:

                                             · FileInputStream (File file): Open a connection to the actual file to create a FileInputStream, the file named by the File object file system file.

                                             · FileInputStream (String name): Open a connection to the actual file to create a FileInputStream, the file name specified by pathname of the file system.

 

               Let's also mention the File class, the object of the File class can represent not only files, but also represents the directory; When you create a file object, you can use it to a file or directory attributes to operate, such as: file name, date last modified, file size, and so on; It should be noted,file object and not directly to the file read / write operations, can only view the file's properties; create formats: file file = new file ( "

                  FileOutputStream class known as file output stream, his role is to write data to a file, we can use it to write the file. The class constructor:

                                             · FileOutputStream (File file): Create a file write data to a file represented by the specified File object in the output stream.

                                             · FileOutputStream (String name): Creates a write data to a file with the specified name of the output file stream.

                   The method commonly used to:

             · Void write (byte [] b): The b.length bytes written to the file from a specified byte array to the output stream.

                                              · Void write (byte [] b, int off, int len): the specified byte array starting at offset off len bytes written to the file output stream.

                                              · Void write (int b): The specified byte is written to the file output stream.

 

        2. BufferedInputStream, BufferedOutputStream (which can be compared with a set top view and, mainly a difference of a buffer )

        BufferedInputStream class called buffered input stream, with a buffer of its own, when data is read first into the buffer, may reduce access to the data source, to improve the efficiency of operation .

                            Construction method:

                                     · BufferedInputStream (InputStream in): Create a BufferedInputStream and saves its argument, the input stream in, for later use.

                                     · BufferedInputStream (InputStream in, int size): Creates a BufferedInputStream specified buffer size, and saves its argument, the input stream in, for later use.

                            Common methods:

                                      · Int read (): Reads the next byte of data from the input stream.

                                      · Int read (byte [] b, int off, int len): from the input stream of bytes starting at the given offset to each byte read the specified byte array.

 

                             BufferedOutputStream type known as a buffer to the output stream, which is itself provided with a buffer, when data is written first into the buffer, data stream buffer. To be noted that the use of BufferedOutputStreamthe written data, to call flush()methods or close()method forced to write the data in the buffer. Otherwise you might not be able to write data.

                             Construction method:

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

                                       · BufferedOutputStream (OutputStream out, int size): Create a new buffered output stream to the specified buffer size having the underlying data into the specified output stream.

                              Common methods:

                                       · Void flush (): Refresh this buffered output stream.

                                       · Void write (byte [] b, int off, int len): the specified byte array starting at offset off len bytes to the output stream of this buffer.

                                       · Void write (int b): The specified byte is written to this buffered output stream.

 

  Jifuryu:

            FileReader, FileWriter; BufferedReader, BufferedWriter (together, said)

                 FileReader class is called to read the stream file, a stream of characters allowed in a file read operation (read from the class file characters one by one, the efficiency is relatively low, it is generally packed into the buffer stream class object in operation . the BufferedReader principal character stream to provide a buffer, it is generally complex BufferReader FileReader to use )

      Construction method:

                                        FileReader(String fileName)

            FileReader(File file)

      Common methods://都是继承自Reader的方法

            int read()

            int read(char[] b)

            int read(char[] b , int off ,int len)

                    FileWriter class called file written to the stream to a stream of characters to write to the file with the similar class FileReader, FileWriter buffered stream class also requires packaging, so FileWriter generally be equipped with BufferedWriter  

                      Construction method:

                         FileWriter(String fileName)
 FileWriter(String fileName, boolean append) //boolean append是否在原有基础上继续写  FileWriter(File file)  FileWriter(File file, boolean append)//boolean append是否在原有基础上继续写

      Common methods:

                         void write(char[] b)
                         void write(char[] b, int off, int len)
                         void write(int b)
                         void write(String str)
                         void write(String str, int off, int len)
                         Writer append(char c)
                         Writer append(CharSequence csq)
                         Writer append(CharSequence csq, int start, int end)

To improve the efficiency of the character stream read, the buffering mechanism is introduced, batch read and write character, increases the efficiency of reading and writing a single character. BufferedReader to read characters used to accelerate the speed, BufferedWriter used to accelerate the speed of writing. BufferedReader and each class has BufferedWriter buffer 8192 characters. When BufferedReader when reading a text file, it will first try to read character data from a file and filled with buffer, and if after using read () method will start reading the buffer. If the buffer underrun, will then read from the file, use BufferedWriter, data is written to the first and will not be output to the destination, but the first memory to buffer. If the data in the buffer is full, the destination will be written once.

      BufferedReaderIs a wrapper class in order to provide efficient reading and design, it can be packaged character stream. May be read from the character input text stream, buffering characters, enabling efficient reading of characters, arrays, and lines.

       Construction method:

         BufferedReader(Reader in)      //创建一个使用默认大小输入缓冲区的缓冲字符输入流。

         BufferedReader(Reader in, int sz)  //Create a character input buffer size specified input stream buffer.

       Common methods:

         int read()                 //读取单个字符。

         int read(char[] cbuf, int off, int len)  //将字符读入数组的某一部分。

         String readLine()             //读取一个文本行(不包括换行符)

      BufferedWriter:

        Construction method:

          BufferedWriter(Writer out)      //创建一个缓冲字符输出流,使用默认大小的输出缓冲区

          BufferedWriter(Writer out, int sz)  //创建一个缓冲字符输出流,使用给定大小的输出缓冲区

        Common methods:

          void write(int c)             //写入单个字符。

          void write(char[] cbuf, int off, int len) //写入字符数组的某一部分。

          void write(String s, int off, int len)   //写入字符串的某一部分。

          void newLine()               //写入一个行分隔符。

          void close()                //关闭此流,但要先刷新它

          void flush()                //刷新该流的缓冲

 

Garbage problem:

        Although the aforementioned plain text data, it must give priority to the use of a character stream, but taking into account coding, you can use the packaging InputStreamReaderof the BufferedReaderpackaging OutputStreamWriterof BufferedWriterto copy files, copy files advantage is that you can specify when the character encoding used does not produce garbled. Here to talk about InputStreamReaderand OutputStreamWriter:

InputStreamReader:它使用指定的字符集读取字节并将它们解码为字符。 它使用的字符集可以通过名称指定,也可以明确指定,或者可以接受平台的默认字符集。每次调用一个InputStreamReader的read()方法都可能导致从底层字节输入流中读取一个或多个字节。为了获得最高效率,通常在BufferedReader中包装InputStreamReader。

使用格式:BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile),"UTF-8"));

OutputStreamWriter:使用指定的字符集将字符编码为字节(是InputStreamReader的逆过程。它的字符集可以由名称指定,也可以接受平台的默认字符集。

   Using the format:BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(destFile), "UTF-8"));

 

Guess you like

Origin www.cnblogs.com/baikaizhuliangshui/p/11704835.html