JAVA 20 Keyboard input

Source: Internet
Author: User

Character Stream Filereaderfilewriter Bufferedreaderbufferedwriter Outputstreamwriterinputstreamreader BYTE stream fileinputstreamfileoutputstream bufferedinputstreambufferedoutputstream system input: InputStream in =System.in;O Utputstream out = System.out; Read Transform stream: Defines the InputStreamReader//byte flow in a character stream system to the bridge of a character stream.  Bufferedinputstream in = (Bufferedinputstream) system.in;  InputStreamReader r = new InputStreamReader (in);  BufferedReader bf = new BufferedReader (r); SOP (Bf.readline ()); Write conversion stream: A bridge of character flow to bytes: outputstreamwriter example: outputstreamwriter OSW = new OutputStreamWriter (new OutputStream ())  Shorthand BufferedReader for two conversion streams BUFR = new BufferedReader (new InputStreamReader (system.in)); BufferedWriter BUFW = new BufferedWriter (new OutputStreamWriter (System.out)); Change the standard input: System.inSystem.out system.setin (New InputStream ()) System.setout (New OutputStream ()) application, print the exception information in the file, Form Management Records      /*1, Source: keyboard entry. Purpose: Console.  2, need: To store keyboard input data into a file. Source: Keyboard. Purpose: File.  3, requirements: you want to print data from a file on the console. Source: File. Purpose: Console. The Basic Law of   flow operation: The most painful thing is that there are many flow objects, and I don't know which one to use.   through three clear to complete.  1, clear source and purpose.   Source: input stream. InputStream  Reader  Purpose: output stream. OutputStream  writer. 2, whether the manipulated data is plain text.   is: Character stream.   NOT: Byte stream.  3, when the system is clear, specify which specific object to use.   Differentiate:  source device by device: memory, hard disk. Keyboard   Destination devices: memory, hard drive, console.   1, stores the data in one text file in another file. Copy the file.   Source: The read stream is used because it is the source. InputStream reader  is not manipulating text files.   YES! At this point you can choose reader  so the system is clear.    then explicitly which object in the system to use.   Clear device: Hard drive. Previous file. The object that can manipulate files in the  reader system is whether filereader   need to improve efficiency: YES!. Join the reader system in buffer bufferedreader.   filereader FR = new FileReader ("A.txt");  bufferedreader bufr = New BufferedReader (FR);      purpose: OutputStream writer  is plain text.   YES! Writer.   Equipment: Hard disk, one file. An object filewriter that can manipulate files in the  writer system.   Whether to improve efficiency: YES!. Added the writer system buffer Bufferedwriter  filewriter FW = new FileWriter ("B.txt");  bUfferedwriter BUFW = new BufferedWriter (FW);   Exercise: Store data from one picture file in another file. Copy the file. To follow the above format to complete their own three clear.   --------------------------------------- 2, requirements: Save keyboard input data to a file.   There are both sources and purposes in this demand.   So separate analysis   Source: InputStream reader  is not plain text? Is! reader   Device: Keyboard. The corresponding object is system.in.  not select reader? System.in corresponds to a byte stream?   Convenient for manipulating the text data of the keyboard. It is most convenient to move to a character stream in a string operation.   So since you've defined reader, then convert system.in into reader.   uses the translator stream in the reader system, Inputstreamreader  inputstreamreader ISR = new InputStreamReader (system.in);    need to improve efficiency? Need! Bufferedreader bufferedreader bufr = new BufferedReader (ISR);     Purpose: OutputStream   writer  is the text stored? Is! Writer.   Device: Hard drive. A file. Use FileWriter.  filewriter FW = new FileWriter ("C.txt");  need to be more efficient? Need.  bufferedwriter BUFW = new BufferedWriter (FW);   **************  Extended, To store the data in a file according to the specified encoding table (UTF-8).    Purpose: OutputStream  Writer  is the text stored? Is! Writer.   Device: Hard drive. A file. Use FileWriter.  However, FileWriter is the default encoding table used. gbk.   But when storing, you need to add the specified encoding table Utf-8. Instead, the specified encoding table can be specified only by the transform stream.   So the object to use is OutputStreamWriter.   The transform stream object is to receive a byte of the output stream. The byte output stream of the file can also be manipulated. Fileoutputstream  outputstreamwriter OSW = new OutputStreamWriter (New FileOutputStream ("D.txt"), "UTF-8"); Does    need to be efficient? Need.  bufferedwriter BUFW = new BufferedWriter (OSW);   so, remember. Convert stream what to use. A bridge between a character and a byte, usually involving a character encoding conversion when,  needs to use a transform stream.    Exercise: Print a text data on the console. To follow the above format to complete their own three clear.    */ import java.io.*;p ublic class Test {  public static void main (string[] args) throws ioexception {  Bufferedinputstream in = (bufferedinputstream) system.in;  StringBuilder sb = new StringBuilder ();  while (true)   {   char ch = (char) in.read ();   if (ch== ' \ n ')     break;   sb.append (CH); }  sop (sb.tostring ());   } public static void Sop ( Object obj)  {  System.out.println (obj); }}   Read system inputImport java.io.*;p Ublic class Test {public static void main (string[] args) throws IOException {Bufferedinputstream in  = (Bufferedinputstream) system.in;  StringBuilder sb = new StringBuilder ();   while (true) {char ch = (char) in.read ();   if (ch== ' \ n ') break;  Sb.append (CH);  } SOP (Sb.tostring ()); } public static void sop (Object obj) {System.out.println (obj);}} using the input and output object case analysis and character encoding conversionImport java.io.*;p Ublic class Test {public static void main (string[] args) throws IOException {//Requirement: Saves keyboard input data to a file.  Entry, character, keyboard input bufferedreader bf = new BufferedReader (new InputStreamReader (system.in)); Output, character, hard disk bufferedwriter bw = new BufferedWriter (New OutputStreamWriter ("C:\\users\\zx\\desktop  \\333.txt ")," UTF-8 ");  Write the file according to Utf-8, also can read the time to specify the encoding String s;   while ((s = bf.readline ())!=null) {if (' over '. Equals (s)) break;   Bw.write (s);   Bw.newline ();  Bw.flush ();  } bw.close (); Bf.close (); } public static void sop (Object obj) {System.out.println (obj);}}

JAVA 20 Keyboard input

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.