J2SE Knowledge Points Induction Note (vii)---Java IO Part 2: Get keyboard input and IO Flow system diagram

Source: Internet
Author: User

J2SE Knowledge Points Induction Note (vii)---Java IO Part 2: Get keyboard input and IO Flow system diagram

--Reprint Please specify Source: Coder-pig


Introduction to this section:


Well, in the previous section we have introduced the use of file and Randomaccessfile classes, and this section we will talk about some

Commonly used things, if you learn C + + or C's friends know, get keyboard input is very simple, scanf () and CIN can

Get the parameters we've entered from the console, or capture the input to the keyboard, and in Java? It looks like we're using

The most is: Syso bar, but this is the printout of the. Well, this section will first explain several console input parameters or data

, and then I'll introduce you to the Java IO Stream architecture diagram to give you a basic understanding of the flow of Io in Java.

Don't say much nonsense, start this section of the content ~



1.Java get keyboard input in several ways:


1) input of a single character:

That is, using the following method although you can get input to the keyboard, but only get one character!!!

If you want to get a Chinese character, forget it, and also to catch the exception ...

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 ("Keyboard input characters are:" +c);} catch (IOException e) {e.printstacktrace ();}}}

Run:





2) Use the BufferedReader class and the InputStreamReader class:

For entering a string, you need to refer to:

The BufferedReader class and the InputStreamReader two bytes of the stream

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 ("Print output characters: \ n" +str);}



3) The Scanner class used:

A new feature of JDK 1.5 , the beauty of its name: simplified scanning, which provides a number of methods, but the most practical of the class

method or get console input, so here only to get console input ...

Scanner provides several common methods:

Next (): get a string;

nextint (): converts the obtained string into an integer of type int;

nextfloat (): convert the obtained string into float type;

Nextboolean (): converts the obtained string into a Boolean type;

nextline (): Read all the remaining contents of the line, including the newline character, and then move the focus to the beginning of the next line


PS: When we press: whitespace, including SPACEBAR, TAB and enter key any one key, even if the end of the input process,

Also pay attention to distinguish between nextline () and other methods of the difference Oh ~


Example code:

Scanner S1 = new Scanner (system.in);//used to differentiate between next () and nextline () System.out.println ("Next () method input string"); String s2 = S1.next (); System.out.println ("Get string:" + s2 + "Length:" + s2.length ()); System.out.println ("nextline () method input string"); String s3 = S1.nextline (); System.out.println ("Get string:" + s3 + "Length:" + s3.length ());//The method of getting the keyboard input string, removing the comment can be used ~://System.out.println ("Please enter a string:");         while (true) {//                 String line = S1.nextline ();//                 if (line.equals ("End"))                 SYSTEM.OUT.PRINTLN ("You entered is:" + line); //         } }

Run:




4) Enter the command line via args:

We all know that the main () method has one parameter:string[] args, which we can pass in the command

Input parameters, and then directly in the main program Args[i] to visit:

Example 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.) The concept of flow:

(Genteel's Professional (ZB) analysis): Data flow is a collection of continuous data, like water in a water pipe,

Water is supplied at one end of the pipe at 1.1, and a continuous flow is seen at the other end of the pipe. The data writer can be a paragraph,

Writes data to a data flow pipeline, which forms a long stream of data sequentially.


Simple to understand:

such as your computer, want to open a PDF file, and your files are stored on the computer's hard disk, open just need to decode the file,

And then display the content to the PDF reader, here is the data----the hard drive is passed to the memory in the form of a stream, where the IO stream is involved!

This also verifies the two characteristics of the stream : Source and destination (send stream, receive stream); there's a direction!

Depending on the direction of the flow can be divided into: output stream and input stream!

Output stream: Typically a stream of data flowing from a computer to a peripheral

Output stream: Typically a stream of data flowing from a peripheral to the computer



If you do not understand this concept, give an example of the image:



The flow is in the direction, if the waterworks to the sewage from the water pipes to your home ...



2.) Java IO Stream architecture diagram:

Above, we divide the flow into the input stream and the output stream in the flow direction!

Next we classify the transmitted data units: byte stream and character stream!

The corresponding base class (parent Class) is:inputstream,outputstream and Reader,writer

It also derives a lot of superclass (subclass): auxiliary flow, buffer stream, filter stream, etc., and look at their architecture diagram below:

PS: In fact, most of the input stream and output stream are one by one pairs, look at the picture will know ~


①inputstream byte stream architecture diagram:




②outputstream byte stream architecture diagram:




③reader Character stream architecture diagram:





④writer Character stream architecture diagram:








Finally say two words:


The classes and interfaces for the flow operations we provide in Java IO are already listed and look a lot, and we don't need to know everything.

Know this thing can be, when used to check the document or Google can be ~

OK, about this section get keyboard input and IO Flow architecture diagram, the next section we learn is:

Byte stream InputStream and the use of the OutputStream class and its subclasses ~ please look forward to








J2SE Knowledge Points Induction Note (vii)---Java IO Part 2: Get keyboard input and IO Flow system diagram

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.