Basic Java Review: Io stream (continued)

Source: Internet
Author: User

Memory stream:

Memory input stream: Memory --->ProgramBytearrayinputstream and chararrayreader

Memory output stream: Program ---> memoryBytearrayoutputstream, chararraywriter

The memory stream does not need to be closed. The read operations are the same as the previous file stream.

 

Print stream:

Printstream printwriter

 
Import Java. io. filenotfoundexception; import Java. io. printstream; public class printdemo {public static void main (string [] ARGs) throws filenotfoundexception {printstream PS = new printstream ("D:/test/well.txt"); PS. write ('A'); PS. print (true); PS. print ("Hello, world ");}}

Print () can be used to output any type of data to a file.

 

Formatted output: printf, same as the C Language

Public class printfdemo {public static void main (string [] ARGs) {string name = "well"; int age = 21; char score = 'a'; system. out. printf ("% s, % d, % C", name, age, score); // Well, 21, }}

 

Pipeline Flow:

Also known as thread communication stream, it is mainly used for communication between two threads.

Pipedoutputstream

Pipedinputstream

 

Standard Input and Output:

System. In And system. Out, which indicate the keyboard and screen by default.

You can redirect it by using setin and setout

Package IO; import Java. io. fileinputstream; import Java. io. ioexception; import Java. io. printstream; public class systemdemo {public static void main (string [] ARGs) throws ioexception {/*** standard stream: * standard input stream: system. in indicates the keyboard input by default * standard output stream: system. out is the screen output by default. *** what if I do not want to print the output on the screen? * What if I don't want to input data through the keyboard? * ** Stream redirection: * Static void setout (printstream out) re-allocates the "standard" output stream. * Static void setin (inputstream in) re-allocates the "standard" input stream. */System. out. println ("12123"); system. out. println ("---- end -----"); system. err. println ("12123"); // redirects the screen output to setout.txt, and the output is not printed on the screen. setout (New printstream ("setout.txt"); system. out. println ("aaaa"); system. out. println ("BBBB "); // ========================================== int I = system. in. read (); // receives data input from the keyboard system. out. println ("keyboard input data =" + (char) I); system. out. println ("----------------- ---------- ");/*** Re-allocate the input source, which was originally a keyboard and is now changed to the day21 .txt file */system. setin (New fileinputstream ("day21 .txt"); byte [] buff = new byte [1024]; int Len = 0; while (LEN = system. in. read (buff ))! =-1) {system. Out. println (new string (buff, 0, Len ));}}}

Scan stream:

 
Import Java. io. file; import Java. io. ioexception; import Java. io. printstream; import Java. util. optional; public class scannerdemo {public static void main (string [] ARGs) throws ioexception {/*** text scanner */Scanner SC = new scanner (system. in); // scan the data input by the keyboard/*** scanner usedelimiter (string pattern) sets the separator mode of this scanner to the mode constructed from the specified string. * // SC. usedelimiter ("\ D");/*** construct (File Source) to construct a new delimiter. The generated value is scanned from the specified file. */System. setout (New printstream ("line.txt"); SC = new partition (new file ("day21 .txt"); int line = 1; while (SC. hasnextline () {// whether there is a system in the next row. out. println (LINE + "" + SC. nextline (); // read the next line ++ ;}}

 

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.