Write the common input and output streams

Source: Internet
Author: User

In accordance with international practice, first of all to introduce the concept of IO and application scenarios.1. What is IOIn Java I/O operations mainly refers to the use of Java for input, output operations. All I/O mechanisms in Java are input and output based on data flow, which represents the flow sequence of characters or byte data. Java I/O streams provide a standard way to read and write data. Any object in Java that represents a data source will provide a way to read and write its data in a data stream.
Java.io is the main software package for most data-flow-oriented input/output classes. In addition, Java also provides support for block transfers, with block IO in the core library Java.nio.advantages and disadvantages of 2.IO flowThe benefits of streaming Io are simple and easy to use, and the disadvantage is low efficiency. Block IO is highly efficient, but programming is more complex.
application of 3.IO flowIn our project, there is never a shortage of streaming applications in the programmer's tool class.    such as transfer files, json interaction, upload download Import export and so on all need to use. such as JSON interaction information: Response.setcontenttype ("Application/text;charset=utf-8");
PrintWriter out = Response.getwriter ();
Out.print (jsonlist);
Out.flush (); such as file download stream: FileInputStream in = new FileInputStream ("path" + "\" + "filename");
OutputStream out = Response.getoutputstream ();
byte buffer[] = new byte[1024];
int len = 0;
while (len = in.read (buffer)) > 0) {
Out.write (buffer, 0, Len);
}
In.close ();
Out.close ();4.IO flow of the detailedThe basic patterns of 4.1 IO streams are roughly the same. The following figure:
4.2 The data in the classification stream sequence can be either raw binary data or specific data that is processed by a certain encoding and conforms to a certain format. So the flow in Java is divided into two types:
1 byte stream: The smallest data cell in the data stream is bytes
2 character stream: the smallest data cell in the data flow is the character, the characters in Java are Unicode encoded, and one character occupies two bytes. Input and output stream in 4.3 Java here is a way to say, a grand introduction: Java.lang.System. This is also often used in the method, the initial learning of the Java language, more or less will be used System.out.println () method to print the console of the nodes, so as to see the data they want to see. (Love to use this before using breakpoints) This method can only use the following three static variables and cannot create new variables on its own.        Out objects are often used, do not have to repeat, here next in the use of objects: system.in read the standard input device data (from the standard input to obtain data, usually the keyboard), its data type is InputStream. Method:
1 int read ()//return ASCII code. If the return value is =-1, the description does not read to any byte read work at the end (The following example feature Comment: Wait for keyboard input, keyboard input What, print out what:); 2 int read (byte[] b)//Read multiple bytes to buffer b The return value is the number of bytes read.
In addition, there are error output, interested in can learn the specific content of their own. 4.4 Sum up the two graphs is a big sum of the other, the method to do a tree-like parsing and processing, looks more cordial and familiar.


IO flow of things profound, far more than a simple summary of the knowledge points listed, so this summary of the nature of the article will continue to update, add. Thanks for the offer of some examples of the large and true destinations.

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.