Java IO review

xiaoxiao2021-03-06  102

Java IO review

Some people inside the forum ask about things about Java IO, the younger brother is not talented. Write something, I hope to give you some inspiration, it is also a throwing brick. ^ _ ^

Java's IO packets support Java's basic input / output (I / O) system, as well as input and output of files. Java's IO section, I intend to elaborate in two parts, first we discuss the foundation of the IO subsystem, and then discuss in-depth parts.

If you have a Java encoding experience, you will find that these programs are not used to use a lot of IO subsystems. In fact, in addition to print () and println (), there is basically no other IO method. The reason is very simple: Most actual Java applications are not based on text, consisters, but based on graphical AppLiaction, which relies on the Java AWT and Swing classes of the user's interaction. At the same time, Java supports the support of the console IO program is not very good, even in a simple example, but based on the text, the Java program in the console also needs us to know.

Java IO is implemented via stream. About streaming, you can understand that it is a "data pipeline". Things that flow in the pipe can be based on bytes, or may be based on characters. It is as if it can flow in the pipe, or it can flow oil. There is also a concept corresponding to stream: input, output device. These devices can be disk files, keyboards (input devices), display (output devices), printers (output devices), network sockets, and more.

Below, let's understand "stream".

Two types of streams are defined in Java: byte, and characters.

Byte stream: Processing the input and output of bytes. Includes contents of reading and writing binary data.

Character stream: Handling the input and output of characters. He uses Unicode encoding and can achieve internationalization. Another advantage of using a character stream is that the character flow is more efficient than by the word.

:::::::::: byte flow :::::::::::::

The byte stream contains two top abstractions: InputStream and OutputStream. As shown below. From the two grams above (gramping from J2SE 1.4.2 DOC), we can see a few points:

1: Two top layers of byte streams are abstract classes, namely, InputStream and OutputStream.

2: Each abstract class has a subclass to achieve a specific function, processing the input and output of different devices.

Abstract Class InputStream and Outpurstream define a practical method, the most important is read () and write (). These two methods are declared as abstract methods in InputStream and OutputStream, and are implemented by sub-stream OverWrite.

:::::::1-0 ::::::::::::::::::: Subclasses to implement processing of Unicode characteristics.

Abstract class Reader and Writer define a practical method, which is read () and Write (). These two methods are declared in Reader and Writer as an abstract method, and is implemented by sub-stream OverWrite.

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 INPUTSTREAMREADER (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-103045.html

New Post(0)