Java Learning Note (ix): Java stream, file, and IO

Source: Internet
Author: User
Tags stringbuffer

Java console input is completed by system.in.

To get a character stream bound to the console, you can wrap the system.in in a BufferedReader object to create a character stream. Need to import this bag

import Java.io.BufferedReader;

Here is the basic syntax for creating BufferedReader:

New BufferedReader (new InputStreamReader (system.in));

Once the BufferedReader object is created, we can use the read () method to read a character from the console or to read a string with the ReadLine () method.

Example: Use the Read () method to continuously read a single character from the console until the user enters "Q".

1 ImportJava.io.*;2 3  Public classtest{4      Public Static voidMain (string[] args)throwsioexception{5         CharC;6BufferedReader br =NewBufferedReader (NewInputStreamReader (system.in));7SYSTEM.OUT.PRINTLN ("input character, press ' Q ' key to exit.) ");8          Do {9c = (Char) Br.read ();Ten System.out.println (c); One} while(c! = ' Q '); A     } -}

Run:

Fffffgfffffg

Example: Read and display the word lines until you enter the word "end".

1 ImportJava.io.*;2 3  Public classtest{4      Public Static voidMain (string[] args)throwsioexception{5BufferedReader br =NewBufferedReader (NewInputStreamReader (system.in));6 String str;7System.out.println ("Enter lines of text."));8System.out.println ("Enter ' End ' to quit."));9          Do {Tenstr =br.readline (); One System.out.println (str); A} while(!str.equals ("End")); -     } -}

Run:

Aaaaaaaabbbbbbbbbbendend

Read and write files

Example: InputStream and OutputStream write read file:

1 ImportJava.io.*;2 3  Public classtest{4      Public Static voidMain (string[] args)throwsioexception{5         /**6 * Write File7          */8File f =NewFile ("Test.txt");9         //build FileOutputStream object, file does not exist automatically newTenFileOutputStream FOP =NewFileOutputStream (f); One         //Build the OutputStreamWriter object, parameters can specify that encoding default encoding is Windows GBK AOutputStreamWriter writer =NewOutputStreamWriter (FOP, "UTF-8"); -         //Write to Buffer -Writer.append ("Chinese Input:"); the         //line Break -Writer.append ("\ r \ n"); -         //Write to Buffer -Writer.append ("中文版")); +         //Close closes the write stream, which writes the contents of the buffer to the file - writer.close (); +         //turn off the output stream A fop.close (); at  -         /** - * read out the file -          */ -         //Building FileInputStream Objects -FileInputStream FIP =NewFileInputStream (f); in         //build the InputStreamReader object with the same encoding as the write -InputStreamReader reader =NewInputStreamReader (FIP, "UTF-8"); to         //Create a string StringBuffer object +StringBuffer SB =NewStringBuffer (); -          while(Reader.ready ()) { the             //go to char and add to StringBuffer object *Sb.append ((Char) Reader.read ()); $         }Panax Notoginseng         //Output File Contents - System.out.println (sb.tostring ()); the         //turn off Read stream + reader.close (); A         //close the input stream and release system resources the fip.close (); +     } -}

Run output:

Chinese input: 中文版

Java Learning Note (ix): Java stream, file, and IO

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.