Operation and introduction of Java output stream

Source: Internet
Author: User

A) file class:

The file class is a class of files in Java that can be used to create directories or to create files. Java in all the IO stream is for the operation of the file, so, want to learn Javaio, learn solid, strongly recommend to first learn the file.

II) Fileinputstream/fileoutputstream: Byte input/output stream:

These two streams are the lowest-level streams, and their reads are in bytes. After the character stream, the buffered stream is packaged on its basis, making it easier to read and write characters.

At the same time, if you are reading non-document content, such as images and videos, the two streams are quite handy.

FileInputStream fis = new FileInputStream (new File ("path"));

File Outputstreamfos = new fileOutputstream(new file ("path"));

But for the flow, a lot of beginners on the concept of input and output is easy to confuse, let me summarize my own convection understanding it:

1, we should stand in the memory angle to look at the input/output

The so-called input stream: is to read the contents of the file into memory, in relation to memory, is the input (that is, the contents of the file input into memory). So we want to read the contents of the file, we should use FileInputStream (input stream) to read the contents of the file into memory, then we can see.

The so-called output stream: Writes the contents of the file, that is, the contents of the memory is written to the file, relative to memory, is the output. So when we write to the file, we should use FileOutputStream (the output stream) to write the content to the file, and then the file will have the content.

III) inputstreamreader/outputstreamwriter: Character input/output stream:

For the input and output stream of character manipulation, you can read and write strings directly.

InputStreamReader ISR = new InputStreamReader (new FileInputStream (New File ("path"));

OutputStreamWriter OSW = new outputstreamwriter (new fileOutputStream(" Path "));

IV) Filereader/filewriter: character input/output stream: They are subclasses of inputstreamreader/OutputStreamWriter, more powerful

These two are the flow of the character operation, we do not need to like the operation of the byte, read characters and write characters to go to a series of cumbersome operations, really can read and write. Convenient and quick.

FileReader FR = new FileReader (new FileInputStream (New File ("path"));

FileWriter FW = new FileWriter (new fileOutputStream(new file ("path"));

Note that FileWriter also has a very important function is that you can append the contents of the file from the end of the file, as long as you create the object, set it, as follows:

FileWriter fw = new FileWriter (new fileOutputStream(new file ("path")), TRUE);// When True, indicates that the content is appended from the end of the file.

V) Bufferreader/bufferwriter: Character input and output stream with buffering:

They are also high-level streams of character manipulation, which can write a lot of content at once, thus achieving efficient purposes, equivalent to batch processing of databases.

BufferEdReader br = new BufferedReader (new FileReader (New FileInputStream (New File ("path")));

BufferedWriter bw = new BufferedWriter (new FileWriter (new fileOutputStream("path") ));

VI) printwriter Print output stream: is also a high-level flow for printing, more convenient, its biggest feature is the ability to set the automatic refresh function. and formatted printing.

PrintWriter pw = new PrintWriter (new Fileoutputwriter (new fileOutputStream("path") ));

Finally, there is a point to remind you that the flow must remember to close, this way to release resources, so the operation of closing the stream is best put in finally to operate.



Operation and introduction of Java output stream

Related Article

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.