Java IO stream

Source: Internet
Author: User
Tags int size

A stream is an ordered set of data sequences that can be divided into input and output streams, depending on the type of operation.

Depending on the type of processing data, the stream can be divided into byte stream and character stream. The related classes are in the Java.io package, and the following are the related classes

1. file input \ Output stream

BYTE stream: FileInputStream and FileOutputStream

FileInputStream and FileOutputStream inherit from InputStream and OutputStream respectively.

/*** Write File*/     Public Static voidTestfileoutputstream () {Try{File File=NewFile ("D:\\test.txt"); if(!file.exists ())                {File.createnewfile (); } FileOutputStream FileOutputStream=Newfileoutputstream (file); String Str= "This is the content"; byte[] b=str.getbytes ();                Fileoutputstream.write (b);              Fileoutputstream.close (); } Catch(IOException e) {e.printstacktrace (); }    }    /*** Read File*/     Public Static voidTestfileinputstream () {Try{File File=NewFile ("D:\\test.txt"); if(!file.exists ())                {File.createnewfile (); } FileInputStream FileInputStream=Newfileinputstream (file); byte[] b=New byte[1024]; System.out.println ("B Before:" +NewString (b)); intLen=fileinputstream.read (b);//Reads up to b.length bytes of the data from the this input stream into an array of bytes.System.out.println ("B After:" +NewString (b,0, Len));              Fileinputstream.close (); } Catch(IOException e) {e.printstacktrace (); }    }

Output:

b before:b After:  the Content

Visible, the Read (byte[] b) method of FileInputStream is read from the stream in B (Reads up to b.length bytes of the data from the thisinput stream into an array of bytes. )

Character stream: FileReader and FileWriter

FileReader and FileWriter inherit from reader and writer respectively

/*** Write File*/     Public Static voidTestfilewriter () {Try{File File=NewFile ("D:\\test.txt"); if(!file.exists ())                {File.createnewfile (); } FileWriter FileWriter=NewFileWriter (file); String Str= "This is the Content2";                Filewriter.write (str);              Filewriter.close (); } Catch(IOException e) {e.printstacktrace (); }    }        /*** Read File*/     Public Static voidTestfilereader () {Try{File File=NewFile ("D:\\test.txt"); if(!file.exists ())                {File.createnewfile (); } filereader FileReader=Newfilereader (file); Char[] cbuf=New Char[1024]; System.out.println ("Cbuf Before:" +NewString (CBUF)); intLen=filereader.read (CBUF);//attempts to read characters into the specified character buffer.System.out.println ("Cbuf after:" +NewString (cbuf,0, Len));              Filereader.close (); } Catch(IOException e) {e.printstacktrace (); }    }

Output:

cbuf before:cbuf After:  the Content2

FileReader Read (char[] cbuf) method, which reads the contents of the stream into the Cbuf

2. Input/output stream with cache

Caching is a performance optimization for I/O, and the cache stream adds a memory buffer to the I/O stream. With buffers, it is possible to execute the skip (), Mark (), and Reset () methods on the stream.

BYTE stream: Bufferedinputstream and Bufferedoutputstream

Bufferedinputstream all InputStream classes are packaged with a buffer to achieve performance optimization. The Bufferedinputstream has two methods of construction:

(1) Bufferedinputstream (InputStream in)

(2) Bufferedinputstream (inputstream in,int size)

The first construction method creates a cache stream with 32 bytes, and the second constructs a method that specifies the size of the created buffer.

The process of Bufferedinputstream reading files is as follows:

Bufferedoutputstream is similar to Bufferedinputstream and has two construction methods,

(1) Bufferedoutputstream (OutputStream out)

(2) Bufferedoutputstream (outputstream out,int size)

The first construction method creates a cache stream with 32 bytes, and the second constructs a method that specifies the size of the created buffer.

The flush () method of the Bufferedoutputstream forces the data in the buffer to be output.

Here's the code:

/*** Write File*/     Public Static voidTestbufferedoutputstream () {Try{File File=NewFile ("D:\\test.txt"); if(!file.exists ())                {File.createnewfile (); } FileOutputStream FileOutputStream=Newfileoutputstream (file); Bufferedoutputstream Bufferedoutputstream=NewBufferedoutputstream (FileOutputStream); String Str= "This is the Content3"; byte[] b=str.getbytes ();                                Bufferedoutputstream.write (b);                   Bufferedoutputstream.close ();        Fileoutputstream.close (); } Catch(IOException e) {e.printstacktrace (); }    }    /*** Read File*/     Public Static voidTestbufferedinputstream () {Try{File File=NewFile ("D:\\test.txt"); if(!file.exists ())                {File.createnewfile (); } FileInputStream FileInputStream=Newfileinputstream (file); Bufferedinputstream Bufferedinputstream=NewBufferedinputstream (FileInputStream); byte[] b=New byte[1024]; System.out.println ("B Before:" +NewString (b)); intLen=bufferedinputstream.read (b);//Reads up to byte.length bytes of the data from the this input stream into an array of bytes.System.out.println ("B After:" +NewString (b,0, Len));                     Bufferedinputstream.close ();        Fileinputstream.close (); } Catch(IOException e) {e.printstacktrace (); }    }

Character stream: BufferedReader and BufferedWriter

BufferedReader and BufferedWriter inherit from reader and writer respectively, have internal cache mechanism, and can input and output in the behavior unit. is the process of reading

The following is a code example:

/*** Write Files*/     Public Static voidTestbufferedwriter () {Try{File File=NewFile ("D:\\test.txt"); if(!file.exists ())                {File.createnewfile (); } FileOutputStream FileOutputStream=Newfileoutputstream (file); OutputStreamWriter OutputStreamWriter=NewOutputStreamWriter (FileOutputStream); BufferedWriter BufferedWriter=NewBufferedWriter (OutputStreamWriter); String Str= "This is the content5\n thissssss";                Bufferedwriter.write (str);                Bufferedwriter.close ();                   Outputstreamwriter.close ();        Fileoutputstream.close (); } Catch(IOException e) {e.printstacktrace (); }    }    /*** Read File*/     Public Static voidTestbufferedreader () {Try{File File=NewFile ("D:\\test.txt"); if(!file.exists ())                {File.createnewfile (); } FileInputStream FileInputStream=Newfileinputstream (file); InputStreamReader InputStreamReader=NewInputStreamReader (FileInputStream); BufferedReader BufferedReader=NewBufferedReader (InputStreamReader); String Str=Bufferedreader.readline ();                System.out.println (str); intI=Bufferedreader.read ();                                System.out.println (i);                Bufferedreader.close ();                   Inputstreamreader.close ();        Fileinputstream.close (); } Catch(IOException e) {e.printstacktrace (); }    }

Output Result:

 This is the content532

Java IO stream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.