Java IO conversion stream

Source: Internet
Author: User

Direct code: Read the keyboard input data code demo:
Import Java.io.ioexception;import Java.io.InputStream; /** * Read the keyboard input data and print it on the console. * * The keyboard itself is a standard input device, * for Java, */public class ReadKey {/** * @param args * @throws IOException */public static void  Main (string[] args) throws IOException {//ReadKey (); Readkey_2 ();   } public static void ReadKey () throws IOException {InputStream in = system.in;  int ch = in.read ();  SYSTEM.OUT.PRINTLN (CH);  int ch1 = In.read ();  System.out.println (CH1);  int CH2 = In.read ();  System.out.println (CH2);   */** * Get user keyboard input data, * and the data into uppercase display on the console, * If the user input is over, the end keyboard entry * * Idea: * 1, because the keyboard input only read one byte, to determine whether it is over, you need to first read the bytes into a string * 2, then you need a container.  You can use StringBuilder * 3 to convert the input data into a string judgment before the user returns. * @throws IOException */public static void Readkey_2 () throws IOException {//create container StringBuilder SB = new Stringbuilde  R ();  Gets the keyboard read stream inputstream in = system.in;  Defines the bytes that the variable record reads to, and loops through the read.  int ch = 0;   while ((ch = in.read ())!=-1) {//before storing, it is necessary to determine whether a newline tag, because the newline tag does not exist if (ch = = ' \ r ') continue; if (ch = = ' \ n ') {String temp = sb.tostring ();    if ("Over". Equals (temp)) break;    System.out.println (Temp.touppercase ());   Sb.delete (0,sb.length ());   }else{Sb.append ((char) ch);  }  } }}

  

Stream conversions: Byte streams change to character stream InputStreamReader
Import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstream;import Java.io.InputStreamReader; public class Transstreamdemo {  /**  * @param args  * @throws IOException *  /public static void main (string[ ] args) throws IOException {  //byte stream  inputstream in = system.in;  A bridge that converts bytes into characters, that is, the transformation stream.  InputStreamReader ISR = new InputStreamReader (in);   Character Stream  BufferedReader bufr = new BufferedReader (ISR);  String line = null;  while ((Line=bufr.readline ())!=null) {   if (' over '. Equals line) {break    ;   }   System.out.println (Line.touppercase ());  } }}

  

 Convert character flow to byte stream OutputStreamWriter
Import Java.io.bufferedreader;import java.io.bufferedwriter;import java.io.ioexception;import java.io.InputStream; Import Java.io.inputstreamreader;import Java.io.outputstream;import java.io.OutputStreamWriter; public class Transstreamdemo {  /**  * @param args  * @throws IOException *  /public static void main (string[ ] args) throws IOException {  //byte stream  inputstream in = system.in;  A bridge that converts bytes into characters, that is, the transformation stream.  InputStreamReader ISR = new InputStreamReader (in);   Character Stream  BufferedReader bufr = new BufferedReader (ISR);   OutputStream out = System.out;  OutputStreamWriter OSW = new OutputStreamWriter (out);  BufferedWriter BUFW = new BufferedWriter (OSW);   String line = null;  while ((Line=bufr.readline ())!=null) {   if (' over '. Equals line) {break    ;   }   Bufw.write (Line.touppercase ());   Bufw.newline ();   Bufw.flush ();  } }}

  

Code rewriting
Import Java.io.bufferedreader;import Java.io.bufferedwriter;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.OutputStreamWriter; public class Transstreamdemo {  /**  * @param args  * @throws IOException *  /public static void main (string[ ] args) throws IOException {  BufferedReader bufr = new BufferedReader (new InputStreamReader (system.in));  BufferedWriter BUFW = new BufferedWriter (new OutputStreamWriter (System.out));   String line = null;  while ((Line=bufr.readline ())!=null) {   if (' over '. Equals line) {break    ;   }   Bufw.write (Line.touppercase ());   Bufw.newline ();   Bufw.flush ();  

  

Copy File Code Demo
Import Java.io.bufferedreader;import Java.io.bufferedwriter;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.OutputStreamWriter; public class Transstreamdemo {  /**  * @param args  * @throws IOException *  /public static void main (string[ ] args) throws IOException {  BufferedReader bufr = new BufferedReader (New InputStreamReader (" A.txt ")));  BufferedWriter BUFW = new BufferedWriter (new OutputStreamWriter (New FileOutputStream ("B.txt"));  String line = null;  while ((Line=bufr.readline ())!=null) {   if (' over '. Equals line) {break    ;   }   Bufw.write (line);   Bufw.newline ();   Bufw.flush ();  } }}  

  

Conversion Flow Summary:    InputStreamReader: Byte-to-character bridge, decoding     OutputStreamWriter: Character-to-byte bridge, coded stream operation Law:    The first reason to understand the operation law is that there are too many objects in the flow, and it is often not known which object is appropriate.     Want to know what objects are used in the development of an object, as long as it is clear by four criteria.  1, clear source and purpose (sinks)     sources: InputStream and reader    purpose: OutputStream and Writer2, to clarify whether the data is plain text data.     Source:            is plain text:reader            not plain text: Input stream    Purpose:            is plain text:writer            not plain Ben: OutputStream at this point it is clear which system to use  3, specific equipment     Source equipment:            HDD: File file            keyboard:system.in            Memory: Array             Network: Socket flow     Destination device:            HDD: File file            Console:system.out            Memory: Arrays             network: Socket stream  4, whether additional features are required.     1) need to be efficient (buffer)             is just add buffer    2) conversion     ... &nbsp ;   common scenarios for using conversion streams:    1, the source or destination device is a byte stream, but the operation is text data, you can use the conversion stream as a bridge, improve the operation of this article convenient     2, the operation text involves the specific encoding table, the conversion flow must be used.    

IO conversion stream for 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.