Scanner and BufferedReader

Source: Internet
Author: User

. Scanner
. Bufferreader:inputstream; OutputStream
. Bufferwriter:
Bufferedreader-->INputstrean-->fileinputstream
Bufferwriter-->outputstrean-->fileoutputstream

Scanner sc =NewScanner (system.in); String Str=Sc.next ();
Scanner reading data is by space character, which includes the SPACEBAR, TAB key, and enter key . As soon as one of them is encountered, the scanner method returns the next input

BufferedReader Stadin=New BufferedReader (New InputStreamReader (system.in)); String input= Stadin.readline ();
N=integer. parseint (input);
ReadLine () is press ENTER to enter to read a row of data, as long as the return before the Enter will be ReadLine (); method returns
to use Bufferreader to enter data of a type other than some characters, it is relatively troublesome, need to pass some xxxx.parsexxx ();
To convert the corresponding data type, although, some trouble, but by contrast on some OJ systems and scanner,bufferreader efficiency is
one times higher than scanner , this gap is conceivable, the more data read, the more obvious effect


BufferedReader and BufferedWriter

1. The Java.io.BufferedReader and Java.io.BufferedWriter classes each have a 8192-character buffer. When BufferedReader reads a text file, it tries to read the character data from the file and place it in the buffer, and then reads from the buffer before using the Read () method. If the buffer data is not enough to be read from the file again, when using BufferedWriter, the data written is not first exported to the destination, but is first stored in the buffer. If the data in the buffer is full, the destination is written out one at a time.

2. When the user input is read directly from the standard input stream system.in, the user enters one character at a time and system.in reads one character. To be able to read the input of a row of users at a time, BufferedReader is used to buffer the characters entered by the user. The ReadLine () method passes the entire line of strings again when it reads the newline character of the consumer.

3. system.in is a bit stream, in order to convert to a character stream, you can use InputStreamReader for character conversion, and then use BufferedReader for it to add buffering capabilities. For example:

Java Code
    1. BufferedReader reader = new BufferedReader (new InputStreamReader (system.in));

The following example demonstrates the use of BufferedReader and BufferedWriter. You can enter characters in text mode, the program will save the input text to the specified file, if you want to end the program, enter the quit string.

     PackageYsu.hxy; ImportJava.util.*; ImportJava.io.*;  Public classBufferedreaderwriterdemo { Public Static voidMain (string[] args) {Try              {                  //buffered system.in input stream//system.in is a bit stream that can be converted to a character stream by InputStreamReaderBufferedReader Bufreader =NewBufferedReader (NewInputStreamReader (system.in)); //Buffer FileWriterBufferedWriter Bufwriter =NewBufferedWriter (NewFileWriter (args[0])); String input=NULL; //One write action per read line                 while(! (input = Bufreader.readline ()). Equals ("Quit") {bufwriter.write (input); //The NewLine () method writes a newline character that is dependent on the operating system, depending on the execution environment's current OS to determine the output type of newline characterBufwriter.newline ();                  } bufreader.close ();              Bufwriter.close (); }              Catch(arrayindexoutofboundsexception e) {System.out.println ("No Files specified"); }              Catch(IOException e) {e.printstacktrace (); }          }      }  

Operation Result:

D:\hxy>java Ysu.hxy.BufferedReaderWriterDemo Test2.txt
Fsdafjads;fa
Fasdfas
Asdfdaf
Asd
FDAs
Hd=
Fa
Df
Asdf
Dasff
Adsf
A
Fasd
Quit

Creates a file test2.txt in the directory and writes the content that you just entered.

BufferedReader in = new BufferedReader (new InputStreamReader (New FileInputStream ("Infilename"));
Whether you read from disk, read from the network, or read from the keyboard, read to memory, that is InputStream.

Write a file
BufferedWriter out = new BufferedWriter (new OutputStreamWriter (New FileOutputStream ("Outfilename"));

Both in and out are memory-centric, memory writes to disk, is out, reads from disk to memory, is in

From the screen number input to memory, in; From the console print out, is System.out

In the same way, socket programming uses more IO, which is explained by server and client respectively.

Server: Encountered a request, network-----> Memory in Server answer, memory-------> Network out
----------------------------------------------------------------------------------------------
Client: Request Service, Memory-----> Network out server answer, network-------> Memory in

The data out of the memory is out of the external peripherals to the memory.

Scanner and BufferedReader

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.