Primary Java IO review (3)

zhaozj2021-02-16  57

Abstract class Reader and Writer define a practical method, the most important thing is read () and

Write (). These two methods are declared in Reader and Writer as an abstract method, and is implemented by sub-stream OverWrite.

The following is a brief introduction to the character stream, inheriting the main subflow of Reader and Wirs.

Character flow function briefly introduces the Reader abstraction class, describing the input Writer abstraction class of the character stream, describing the input stream of the character stream STINGRITER writes the input stream of the string FileReader from the file FileWriter The output stream of the write file PrintWriter contains the most common output () and println () output stream PushbackReader returns a character to the input stream, which is mainly used for the compiler to implement the PiPedReader output pipeline PiPedWriter input pipeline ChararrayReader read input from the character array flow CharArrayWriter written character array output stream BufferReader buffered input stream BufferWriter buffered output stream FilterReader achieved InputStream Interface FilterWriter achieved OutputStream Interface InputStreamReader converts a byte stream input character OutputStreamWriter converts a byte character output stream

Contrast two tables, do you have any findings? ~~

^ _ ^, Is there a lot of class names and roles, yes, yes! The differences in these methods are only in one for bytes, and one is for characters.

Simply understand the concept of stream and the various subclasses of the two streams. Below we have implemented above through some simple programs, after all, the example is something that is a good understanding.

1: Read the input of the console. . . . . .

The specific statement is as follows:

BufferedReader Br = New BufferedReader (NEW INPUTSTREADER (System.in);

The following statement is explained below:

System.in - "Read the data of the console, he read the byte stream.

InputStreamReader - "Its role is to convert byte stream to a character stream. Its constructor is:

InputStreamReader (InputStream InputStream)

BufferedReader - "Put the input stream buffer to improve processing efficiency.

The above statement can also be written into the following form more easily understood. .

InputStreamReader INS = New InputStreamReader (System.in);

BufferedReader Br = New BufferedReader (INS);

2: Console output. . . . . . . . . . . . .

The console output can be used with print () and println (), which is easy to use. Other can be used

Write () method to achieve the output. Its constructor is:

Void Write (int Byteval)

Here is an example program:

Class Writeexample

{

Public static void main (string args [])

{

Int n;

A = 'a';

System.out.write (a);

System.out.write ('/ n');

}

}

3: Read characters. . . . . . . . . . . . . . . . . . Use the previous BufferedReader definition to read () to read the return value INT, so in order to return a char, switching. READ () constructor:

Int Read () THROWS IOEXCEPTION

The specific procedures are as follows:

Import java.io. *;

Class READEXAMPLE

{

Public static void main (string args []) THROWS IOEXCEPTION

{

Char a;

BufferedReader Br = New BufferedReader (NEW INPUTSTREADER (System.in);

System.out.println ("Enter Q to Quit.");

a = (char) br.read (); // Read in the input stream (char) A

IF (a! = 'Q')

System.out.println (a);

}

}

4: Read the string. . . . . . . . . . . . . . . . . . . . .

Use the function: readline () Its common form is:

String readline () THROWS IOEXCEPTION

Import java.io. *

Class ReadlineException

{

Public static void main (string args []) THROWS IOEXCEPTION

{

String Str;

BufferedReader Br = New BufferedReader (NEW INPUTSTREADER (System.in);

System.out.println ("Enter A String Line, Q to Quit.");

Str = B.Readline ();

IF (str! = Q)

System.out.println (STR);

}

}

5: About the reading and writing of documents. . . . . . . . . . . . . . . . . . . . . . . . . .

Java provides read and write to files, but it is important to note that all files are both bytes. But Java allows us to package both bytes based on characters-based objects.

The stream of two to files is: FileInputStream and FileOutputStream. These two classes of constructor are:

FileInputStream (String FileName) Throws FilenotFoundExcepiont

FileOutputStream (String FileName) Throws FilenotFoundException

This two classes define several functions:

After the file is completed, use the close () method to close the file.

Void Close () THROWS IOEXCEPTION

Used to read a file, use read () to read the byte stream, if you read the end of the file

READ () will return -1.

Int Read () THROWS IOEXCEPTION

Used to write a file, use Write () to write the byte stream to the required file, this method writes the parameter into the required file, but pay attention to only 8 bytes to write files. Here is an example program:

Import java.io. *;

Class copyfile {

Public static void main (string args []) THROWS IOEXCEPTION

{

Int n;

FileInputstream Fin;

FileOutputstream fout;

Try

{

Try

{

FIN = New FileInputStream (args [0]);

}

Catch (FilenotFoundException E)

{

System.err.Println (e);

Return;

}

Try

{

Fout = New FileOutputStream (Args [1]);

}

Catch (FilenotFoundException E)

{

System.err.Println (e);

Return;

}

}

Catch (arrayinedxoutboundsexception e)

{

System.err.Println (e);

Return;

}

Try

{

DO

{

n = fin.read ();

IF (n! = -1)

Fout.write (n);

}

While (n! = -1);

}

Catch (IOException E)

{

System.err.Println (e);

}

Fin.close ();

Fout.close ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-23388.html

New Post(0)