Java-based bufferedreader

Source: Internet
Author: User

1. Receive keyboard input:

 

Bufferedreader Buf = new bufferedreader (New inputstreamreader (system. In ));

String Password = "";

Try {

Password = Buf. Readline ();

} Catch (ioexception E)

{

System. Out. println (E );

System. Exit (0 );

}

 

 

Bufferedreader
Java. Lang. Object
Java. Io. Reader
Java. Io. bufferedreader
Specific Method:

 void
close

()

Close the stream.


 void
mark

(int readAheadLimit)

Mark the current position in the stream.


 boolean
markSupported

()

Determine whether the stream supports the mark () operation (which must be supported ).


 int
read

()

Read a single character.


 int
read

(char[] cbuf,
int off,
int len)

Read characters into a part of the array.


 String

readLine

()

Read a text line.


 boolean
ready

()

Determine whether the stream is ready for reading.


 void
reset

()

Resets the stream to the latest tag.


 long
skip

(long n)

Skip characters.

Http://www.gznc.edu.cn/yxsz/jjglxy/book/Java_api/java/io/BufferedReader.html


Inputstreamreader is a bridge between inputstream and reader. Because system. In is a byte stream, it needs to be packaged and then converted into a bytes stream for bufferedreader to use.


The Java. Io. bufferedreader and Java. Io. bufferedwriter classes each have a buffer of 8192 characters. When
When bufferedreader reads a text file, it tries its best to read character data from the file and put it into the buffer zone. If the read () method is used, it will first read the data from the buffer zone. For example
When bufferedwriter is used, the written data is not output to the destination, but stored to the buffer. If the number in the buffer zone
When the data is full, the destination will be written once. For example, a file can reduce input/output operations on the hard disk through a buffer to improve file access efficiency.

Previously, bufferedreader was used to obtain user input. When a user input is directly read from the standard input stream system. In, each user input a word
System. In. To read a row of user input at a time, bufferedreader is used to buffer user input characters.
When the Readline () method reads the user's line feed character, it passes in the entire line of string again.

System. In is a single-bit stream. In order to convert it into a bytes stream, you can use inputstreamreader to convert its characters, and then use bufferedreader to provide a buffer function for it. For example:

BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));

Example 14.19 demonstrates the use of bufferedreader and bufferedwriter. You can enter characters in text mode. The program stores the entered text in the specified file. to end the program, enter the quit string.

Tutorial 14.19 bufferedreaderwriterdemo. Java

package onlyfun.caterpillar;
import java.io.*;
Public class bufferedreaderwriterdemo {
Public static void main (string [] ARGs ){
Try {
// Buffer the system. In input stream
Bufferedreader bufreader =
New bufferedreader (
New inputstreamreader (system. In ));
// Buffer the output stream of filewriter characters
Bufferedwriter bufwriter =
New bufferedwriter (New filewriter (ARGs [0]);
            String input = null;
// Write each row
While (! (Input =
Bufreader. Readline (). Equals ("quit ")){
Bufwriter. Write (keyin );
// Newline () method writes line breaks that are dependent on the Operating System
Bufwriter. newline ();
}
Bufreader. Close ();
Bufwriter. Close ();
}
Catch (arrayindexoutofboundsexception e ){
System. Out. println ("no file specified ");
}
Catch (ioexception e ){
E. printstacktrace ();
}
}
}

The line feed character varies depending on the operating system. It is/R/N in windows,/N in Linux, And/R in Mac OS, you can use the newline () method to determine which newline character to output according to the operating system at that time.

 

 

 

Common Conversions

Filereader --> bufferedreader
Bufferedreader in = new bufferedreader (New filereader ("text. Java "));
Inputstream --> inputstreamreader --> bufferedreader
Bufferedreader in = new bufferedreader (New inputstreamreader (system. In ));
String --> byte [] --> bytearrayinputstream --> datainputstream
Datainputstream in = new datainputstream (New bytearrayinputstream (Str. getbytes ()));
Fileinputstream --> bufferedinputstream --> datainputstream
Datainputstream in = new datainputstream (New bufferedinputstream (New fileinputstream ("data.txt ")));
Filewriter --> bufferedwriter --> printwriter
Printwriter PW = new printwriter (New bufferedwriter ("text. Out "));
System. Out (printstream) --> printwriter
Printwriter PW = new printwriter (system. Out, true );
Fileoutputstream --> bufferedoutputstream --> printstream
Printstream PS = new printstream (New bufferedoutputstream (New fileoutputstream ("text. Out ")));
Fileoutputstream --> bufferedoutputstream --> dataoutputstream
Dataoutputstream dos = new dataoutputstream (New bufferedoutputstream (New fileoutputstream ("data.txt ")));

Program example

Import java. Io .*;
Public class iostreamdemo {
Public static void main (string [] ARGs) throws ioexception {
1. To open a file to read characters, you must first create a filereader with a string or file object.
To increase the speed, you should buffer the file, so you have to give the reference of filereader to bufferedreader.
Bufferedreader provides the Readline () method. When you read the end of a file, Readline () returns a null value, and then exits the while () loop.
String Sb is used to accumulate the file content (add a line break "/N" because Readline () will remove them ).
Finally, use close () to clear the buffer.
Bufferedreader in = new bufferedreader (New filereader ("iostreamdemo. Java "));
String S, S2 = new string ();
Stringbuffer sb = new stringbuffer ();
While (S = in. Readline ())! = NULL)
{Sb. append (s );
SB. append ("/N ");
}
In. Close ();
S2 = sb. tostring ();
2. Use System. In to generate a stream that can read the input from the console. System. In is an inputstream,
Bufferedreader requires a reader as the parameter, so it must be transferred through inputstreamreader.
Java follows the standard I/O model and provides syetem. In, system. Out, And system. Err.
System. Out is a pre-processed and encapsulated printstream object.
System. Err is also a printstream;
System. In is an unprocessed inputstream.
That is to say, although you can directly write to system. Out and system. Err, to read system. In, you must first process it.
Bufferedreader stdin = new bufferedreader (New inputstreamreader (system. In ));
System. Out. Print ("enter a line :");
System. Out. println (stdin. Readline ());
3. Use string S2 as the parameter to create a stringreader. Then, use the stringreader's read () method to read the characters and then send them to the console. Read () treats the read bytes as int, so to print them properly, you must convert them into char.
Stringreader in2 = new stringreader (S2 );
Int C;
While (C = in2.read ())! =-1)
System. Out. Print (char) C );
4.
To read formatted data, you must use datainputstream. datainputstream is a byte-oriented I/O class, not a char-oriented
. Therefore, you can only use inputstream from the beginning to the end. Of course, you can treat everything as a byte. Then read it using inputstream. But here is
String. To treat string as a byte array. You can use the getbytes () method of string. And bytearrayinputstream is OK
Processing byte arrays.
Try {
Datainputstream in3 = new datainputstream (
New bytearrayinputstream (s2.getbytes ()));
While (true)
System. Out. Print (char) in3.readbyte ());
} Catch (eofexception e ){
System. Err. println ("End of stream ");
}


Conversion between string and char:
Convert string to char array
Char CHR [] = C. tochararray ();
Char to string:
String (CHR );

Or string STR = string. valueof (c); C is Char;

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.