Operation of Io stream in Java

Source: Internet
Author: User

Read the conversion stream-read keyboard input keyboard input a row of data and print its capitalization, found to read a row of data principle.
That is the ReadLine method.
Can you directly use the ReadLine method to complete the reading of the keyboard input line of data?
The ReadLine method is a method in the character stream BufferedReader class.
The Read method of keyboard input is the method of byte stream inputstream.
Is it possible to convert byte streams into character stream and then use the ReadLine method of the character stream buffer?

 Public classTransstreamdemo { Public Static voidMain (string[] args) {//Get Keyboard Entry ObjectsInputStream is =system.in;//Converts a byte stream object to a character stream object, using the transform stream InputStreamReaderInputStreamReader ISR =NewInputStreamReader (IS);//in order to increase efficiency, the string is fed into the buffer, using the BufferedReaderBufferedReader br =NewBufferedReader (ISR); String Line=NULL; Try {             while(Line =br.readline ())! =NULL) {                if("Over". Equals (line)) Break;            System.out.println (Line.touppercase ()); }        } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}
View Code

Write conversion stream-converts character flow to byte stream

 Public classTransStreamDemo2 { Public Static voidMain (string[] args) {//InputStream is=system.in;//InputStreamReader isr=new InputStreamReader (IS);//BufferedReader br=new BufferedReader (ISR);//can be simplified to//This is the most commonly read keyboard inputBufferedReader br =NewBufferedReader (NewInputStreamReader (system.in));//outputstream os =system.out;//OutputStreamWriter OSW = new OutputStreamWriter (OS);//bufferedwriter bw = new BufferedWriter (OSW);//can be simplified toBufferedWriter BW =NewBufferedWriter (NewOutputStreamWriter (System.out)); String Line=NULL; Try {             while(line = Br.readline ())! =NULL) {                if("Over". Equals (line)) Break;//Print UppercaseBw.write (Line.touppercase ());                Bw.newline ();            Bw.flush (); }        } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}
View Code

Flow Operation Law----keyboard input data stored in a file

 Public classTransStreamDemo3 { Public Static voidMain (string[] args)throwsIOException {bufferedreader br=NewBufferedReader (NewInputStreamReader (system.in)); BufferedWriter BW=NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream ("E:/out.txt"), "Utf-8")); String Line=NULL;  while(line = Br.readline ())! =NULL) {            if("Over". Equals (line)) Break;            Bw.write (line); System.out.println ("Write succeeded");            Bw.newline ();        Bw.flush (); }    }}
View Code

Prints the data for a file on the console.

 Public classTransStreamDemo4 { Public Static voidMain (string[] args)throwsIOException {bufferedreader br=NewBufferedReader (NewInputStreamReader (NewFileInputStream ("E:/out.txt"), "Utf-8")); BufferedWriter BW=NewBufferedWriter (NewOutputStreamWriter (System.out)); String Line=NULL;  while(line = Br.readline ())! =NULL) {bw.write (line);            Bw.newline ();        Bw.flush (); }    }}
View Code

Operation of Io stream in Java

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.