Standard input stream and standard output stream for Java 21-11 IO streams

Source: Internet
Author: User

Standard input/output stream
Two member variables in the system class:
public static final InputStream in "standard" input stream.
public static final PrintStream out "standard" output stream.

Pnputstream is = system.in;
PrintStream PS = System.out;

System.out; Standard output stream:

You can see it from the top, actually.

        System.out.println ("HelloWorld");

actually equals

        PrintStream PS = System.out;        Ps.println ("HelloWorld");

Attention:  

1 printstream PS = System.out; 2 //The following two methods, in fact, is a method does not exist, note: () nothing inside.  3ps.print (); 4 System.out.print ();

Let's play something interesting:

How do I make a byte stream into a character stream to manipulate it?

We can play by converting streams, byte stream → convert stream → character stream

1      Public Static voidMain (string[] args)throwsIOException {2                 //get standard input stream3                 // //PrintStream PS = System.out;4                 // //outputstream OS = PS;5                 //outputstream os = system.out;// polymorphic6                 // //we use a character stream to wrap the character stream, the surface is a stream of characters, in fact, the bottom is the byte stream (standard output stream)7                 //OutputStreamWriter OSW = new OutputStreamWriter (OS);8                 //bufferedwriter bw = new BufferedWriter (OSW);9BufferedWriter BW =NewBufferedWriter (NewOutputStreamWriter (Ten System.out)); One  ABw.write ("Hello"); - bw.newline (); -Bw.write ("World"); the bw.newline (); - Bw.flush (); -                  - bw.close (); +}

。。。 If you're going to die, just understand.

system.in; Standard input stream:


The input stream and the output stream are about the same. Here's an interesting thing:

To JDK1.7, there are 3 ways to achieve keyboard entry:

1, the main method of the args receive parameters:    

Java HelloWorld Hello World Java

  2, Scanner (JDK1.5 later)

1 New Scanner (system.in); 2 String s = sc.nextline (); 3 int x = sc.nextint (); 4 ...

Well, before JDK1.5, this is the way:

3, through the character buffer stream packaging standard input stream implementation:

1      Public Static voidMain (string[] args)throwsIOException {2         // //get standard input stream3         //InputStream is = system.in;4         //However, this can only fetch one byte at a time. And what we want is to get one row of data at a time5         //To achieve this, first you need to know which way to read one row of data at a time?6         //ReadLine ()7         //and this method is in the BufferedReader class, so we should create the BufferedReader object, but the underlying is still using the standard input stream8         //bufferedreader br = new BufferedReader (is);9         //But there's still an error here. The reason is that the character buffer stream can only be manipulated for character streams, and InputStream is a byte stream, so you cannot useTen         //If you want to use it, you can only convert the byte stream to a character stream and then manipulate it through the character buffer stream. One          A         //InputStreamReader ISR = new InputStreamReader (is); -         //BufferedReader br= New BufferedReader (ISR); These two sentences can be written in the following sentence -BufferedReader br =NewBufferedReader (NewInputStreamReader (system.in)); the  -System.out.println ("Please enter a string:"); -String line =br.readline (); -System.out.println ("The string you entered is:" +Line ); +  -System.out.println ("Please enter an integer:"); +         //int i = Integer.parseint (Br.readline ()); Aline =br.readline (); at         inti =Integer.parseint (line); -System.out.println ("The Integer you entered is:" +i); -}

Conclusion:... Let's just use scanner.

  

Standard input stream and standard output stream for Java 21-11 IO streams

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.