J2SE knowledge point summary note (7) --- Java IO Part 2: Get the keyboard input and IO stream system diagram, j2se --- java

Source: Internet
Author: User

J2SE knowledge point summary note (7) --- Java IO Part 2: Get the keyboard input and IO stream system diagram, j2se --- java

J2SE knowledge point summary note (7) --- Java IO Part 2: Get the keyboard input and IO stream system diagram

-- Reprinted with the source: coder-pig


This section introduces:


Okay. In the previous section, we will introduce the usage of the File and RandomAccessFile classes. In this section, we will talk about some

Common things: Anyone who has learned C ++ or C knows that it is easy to get the keyboard input, scanf () and cin.

Get the parameters we entered from the console, or capture the keyboard input ~ What about Java? It seems we use

The most is: syso, but this is the output .. well, this section will first explain several console input parameters or data

Next, I will introduce you to the Java IO stream system diagram, so that you can have a basic understanding of the stream I/O used in Java.

Not to mention nonsense. Start this section ~



1. Java methods for obtaining keyboard input:


1) single character input:

That is to say, although the keyboard input can be obtained using the following methodGet only one character !!!

If you want to obtain a Chinese character, forget it and capture exceptions...

The Code is as follows:

Import java. io. IOException; public class StringTest {public static void main (String [] args) {char c; try {c = (char) System. in. read (); System. out. println ("the character entered by the keyboard is:" + c);} catch (IOException e) {e. printStackTrace ();}}}

Run:





2) use the BufferedReader class and InputStreamReader class:

If the input is a string, it involves:

The BufferedReader class and InputStreamReader are two byte streams.

The Code is also simple:

Public static void main (String [] args) {BufferedReader br = new BufferedReader (new InputStreamReader (System. in); String str = null; System. out. println ("enter a string of characters:"); try {str = br. readLine ();} catch (IOException e) {e. printStackTrace ();} System. out. println ("printed output character: \ n" + str );}



3) usage of the category class:

JDK 1.5A new feature, called simplified scanning, provides many methods, but is the most practical.

The method is to get the console input, so here only to get the console input...

Pipeline provides several common methods:

Next ():Returns a string;

NextInt ():Converts the obtained string to an integer of the int type;

NextFloat ():Converts the obtained string to the float type;

NextBoolean ():Convert the obtained string to the boolean type;

NextLine ():Read all the remaining contents of the row, including line breaks, and move the focus to the beginning of the next line.


PS: When we press: space character, including space key, Tab key and Enter key, even if the input process ends,

In addition, note the differences between nextLine () and other methods ~


Sample Code:

Repeated s1 = new partition (System. in); // used to distinguish next () from nextLine () System. out. println ("next () method input String"); String s2 = s1.next (); System. out. println ("string:" + s2 + "Length:" + s2.length (); System. out. println ("nextLine () method input String"); String s3 = s1.nextLine (); System. out. println ("resulting string:" + s3 + "Length:" + s3.length (); // obtain the input string of the keyboard. Remove the comment and you can use it ~ : // System. out. println ("Enter the String:"); // while (true) {// String line = s1.nextLine (); // if (line. equals ("end") break; // System. out. println ("You entered:" + line );//}}

Run:




4) input in the command line through args:

We all know that the main () method has a parameter:String [] argsIn the command

Input parameters in the line, and then directly access the args [I] in the main program:

Sample Code:

public class JavaTest2 {public static void main(String[] args) {for (int i = 0; i < args.length; i++)System.out.println(args[i]);}}

Run:





2. Java IO stream system diagram: 1.) concept of stream:

(ZB): Data streams are a collection of continuous data, just like water flow in a pipe,

Water is supplied at one end of the pipe at 1.1 points, while a continuous flow of water is seen at the other end of the pipe. The data writing program can be a segment,

A Data Segment writes data to the Data Stream pipeline sequentially, forming a long data stream.


Easy to understand:

For example, if you want to open a PDF file on your computer, and your file is stored on the hard disk of the computer, you need to decode the file to open it,

Then, display the content to the PDF reader. Here is the data on the hard disk-> transfer the content to the memory in the form of a stream-> I/O Stream!

This also verifies the two features of the stream.: Source and target (send stream, receive stream); direction!

The stream direction can also be divided into: output stream and input stream!

Output stream: data streams from computers to peripherals

Output stream: data streams from peripherals to computers



If you do not understand this concept, let's give an image example:



There is a direction for the flow, if the water plant will discharge domestic sewage from the tap water pipe to your home...



2.) Java IO stream system diagram:

Above, we split the stream into an input stream and an output stream by the direction of the stream!

Next we will classify data units by transmission: byte stream and byte stream!

The corresponding base class (parent class) is:InputStream, OutputStream and Reader, Writer

Many super classes (subclasses) are also derived: Auxiliary streams, buffered streams, and filtered streams. Let's take a look at their architecture diagram below:

Ps: in fact, most of the input and output streams are paired one by one. You can see the figure ~


① Structure of the InputStream byte stream system:




② Structure of the OutputStream byte stream system:




③ Structure of the Reader internal stream system:





④ Structure diagram of Writer internal stream:








The last two sentences are as follows:


Classes and interfaces related to stream operations provided by Java I/O have been listed. It seems that we do not need to master them all,

You can check the document or Google when you know it ~

Now, for more information about how to obtain the keyboard input and I/O flow architecture, see the following section:

Usage of byte stream InputStream and OutputStream classes and their subclasses ~ Coming soon








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.