A huge family that is constantly updated-Java IO

Source: Internet
Author: User

A huge family that is constantly updated-Java IO
1. Concept of stream

In Java APIs, the object that can read a byte sequence is called an input stream, and the object that can write a byte sequence to it is called an output stream. The source and destination of these byte sequences can be files, but they can also be network connections or even memory blocks. The abstract concept of stream is used in the I/O class library of programming language. It represents any data source object that can generate data or the receiving end object that can receive data.

A simple understanding: a stream is a collective term for data and data processing.

Stream operations focus on data sources, targets, and processes.

These data sources include:

1) byte array

2) String object

3) File

4) "Pipeline", which works in a similar way as the actual pipeline, that is, output from one input segment and one output from the other.

5) a sequence composed of other types of streams so that we can combine them into a stream.

6) Other data sources, such as Internet connections.

Ii. stream classification

1. stream direction: input stream and output stream

An object that can read a byte sequence is called an input stream, and an object that can write a byte sequence to it is called an output stream. Both inbound and outbound are relative to the program itself (not equivalent to the stream itself, because the "inbound" in the program reads data from the stream to the program, for example, if you use a file in a program to save data, write data to the stream and save it to the file. At this time, the stream is the output stream; the stream that reads data from a stream to a program is called an input stream.

 

The above concepts also reflect the characteristics of this type of stream: the input stream can only be read, and the output stream can only be written.

1. byte stream and byte stream by data unit

On a computer disk, all data is transmitted and stored in bytes, including images, videos, documents, and other data. A byte occupies 8-bit binary (8 bit), while a character occupies different storage space according to the encoding method. One sentence is suitable for the present: the basic thing is omnipotent. Therefore, byte streams can process data of any type, while character streams can only process data of the character type.

2. By function: node stream and filter stream

The node stream uses the original stream class for operations, and the filter stream is modified based on the node stream for more functions. The decorator mode is used here.

3. input stream and output stream hierarchies top-level parent classes of byte streams are abstract classes InputStream and OutputStream. the hierarchies of input and output streams are as follows:

 

The hierarchical structure of Reader and Writer is as follows:

 

The I/O class in the Java class library is divided into two parts: input and output, which can be viewed in the class hierarchy in the JDK document. By inheritance, any class derived from InputStream or Reader contains the basic method named read (), which is used to read a single byte or byte array. Similarly, any class derived from the OutputStream or Writer class will contain a basic method named write () to write a single byte or byte array. However, we usually do not use these methods. They exist because other classes can use them to provide more useful interfaces. Therefore, we seldom use a single class to create stream objects. Instead, we provide the expected functions by aggregating multiple objects (this is the decorator design mode ). In fact, the main confusing reason for the Java "stream" class library is that multiple objects need to be created to create a single result stream.

FilterInputStream and FilterOutputStream are two classes used to provide a modifier class interface to control the output stream of a specific input stream. Their names are not intuitive. FilterInputStream and FilterOutputStream are derived from the accumulated InputStream and OutputStream in the I/O class library respectively. These two classes are necessary conditions for the decorator (so as to provide common interfaces for all objects being modified ).

The FilterInputStream class can accomplish two completely different tasks. DataInputStream allows us to read different basic data types and String objects (all methods start with "read", such as readByte () and readFloat ). With the corresponding DataInputStream, we can use the data "stream" to migrate basic data types from one place to another.

Other FilterInputStream classes are used to internally modify the InputStream behavior mode: whether to buffer or retain the rows it has read (allow us to query the number of rows or set the number of rows ), and whether to push a single character back to the input stream. The last two classes seem to be more like creating a compiler (they may be added to support the "build compiler with Java" experiment), so they are not used in general programming.

We buffer the input almost every time-no matter what I/O device we are connecting to, the I/O class library caches the non-buffered input (rather than the buffered input) as a special case (or just a method call), it is more reasonable.

Iv. Introduction to Java IO Classification 4.1-byte stream 4.1.1 byte input stream

InputStream indicates the superclass of all classes in the byte input stream,

FileInputStream obtains the input bytes from a file in the file system.

ByteArrayInputStream contains an internal buffer that contains the bytes read from the stream. Internal counter trackingreadThe next byte to be provided by the method. CloseByteArrayInputStreamInvalid. Methods In this class can still be called after the stream is closed without generating anyIOException.

The input stream of the PipedInputStream pipeline should be connected to the output stream of the pipeline. The input stream of the pipeline provides all data bytes to be written to the output stream of the pipeline. Generally, data is transmitted by a thread fromPipedInputStreamRead the object and write it to the correspondingPipedOutputStream. We do not recommend that you try to use a single thread for these two objects, because the thread may be deadlocked. The input stream of the MPs queue contains a buffer. You can separate read and write operations within the buffer range. If the thread that provides data bytes to the output stream of the connection pipe no longer existsDamaged.

FilterInputStream contains some other input streams, which are used as their basic data source. It can directly transmit data or provide some additional functions.

ObjectInputStream deserializes the basic data and objects previously written using ObjectOutputStream.

SequenceInputStream indicates the logical concatenation of other input streams. It starts from the sorted set of the input stream, and reads from the first input stream until it reaches the end of the file, then reads from the second input stream, and so on, until the end of the file containing the last input stream is reached.

BufferedInputStream adds some functions for another input stream, that is, buffer input and supportmarkAndresetMethod capabilities. In the createBufferedInputStreamAn internal buffer array is created. When reading or skipping byte, you can fill in the internal buffer again from the input stream as needed and fill in multiple bytes at a time.markOperation records a point in the input stream,resetThe operation reads the last byte from the input stream before obtaining the new byte.markAll bytes read after the operation.

DataInputStream data input stream allows applications to read Basic Java data types from the underlying input stream in a machine-independent manner. Applications can use the data output stream to write data that is later read by the data input stream.

PushbackInputStream adds performance to another input stream, that is, the ability to push back or unread one byte.

4.2.2 byte output stream

OutputStream this abstract class is a superclass that represents all the classes of the output byte stream.

ByteArrayOutputStream implements an output stream, and the data is written into a byte array. The buffer will automatically increase as data is constantly written. AvailabletoByteArray()AndtoString()Obtain data. CloseByteArrayOutputStreamInvalid.

The FileOutputStream file output stream is used to write dataFileOrFileDescriptor.

FilterOutputStream is a superclass that filters all classes of the output stream.

ObjectOutputStream writes the basic data types and graphics of Java objects to OutputStream.

PipedOutputStream connects the output stream of the pipeline to the input stream of the pipeline to create a communication pipeline. The output stream of the MPs queue is the sender of the MPs queue.

BufferedOutputStream implements buffered output streams. By setting this output stream, the application can write each byte to the underlying output stream without calling the underlying system for each byte write.

DataOutputStream data output stream allows applications to write Basic Java data types to the output stream in an appropriate manner. Then, the application can use the data input stream to read data.

4.2 streams 4.2.1 character input stream

The abstract class used by Reader to read the livestream.

BufferedReader reads text from the character input stream and caches each character to efficiently read characters, arrays, and rows.

LineNumberReader tracks the buffer character input stream of the row number. This class defines methods setLineNumber (int) and getLineNumber (), which can be used to set and obtain the current row number respectively.

CharArrayReader implements a character buffer zone that can be used as a character input stream.

InputStreamReader is a bridge between byte stream and byte stream: It reads bytes using the specified charset and decodes them into characters. Its character set can be specified by name or explicitly specified, or it can accept the default Character Set of the platform.

A convenient class used by FileReader to read character files. This constructor assumes that the default character encoding and the default byte buffer size are both appropriate.

The source of StringReader is a character stream of a string.

The character input stream transmitted by PipedReader.

FilterReader is an abstract class used to read filtered streams.

PushbackReader allows a forward stream reader that pushes characters back to the stream.

4.2.2 character output stream

The abstract class written by Writer to the livestream.

BufferedWriter writes text to the character output stream, buffering each character, so as to provide efficient writing of a single character, array, and string.

CharArrayWriter implements a character buffer zone that can be used as Writer. The buffer zone automatically increases as data is written to the stream. You can use toCharArray () and toString () to obtain data. The call to close () on this class is invalid.

OutputStreamWriter is a bridge between the bytes stream and the byte stream. You can use the specified charset to encode the characters in the stream into bytes. The character set used by the platform can be specified or explicitly specified by the name. Otherwise, the default Character Set of the platform will be accepted.

FileWriter is a convenient class for writing character files.

The character output stream transmitted by PipedWriter.

PrintWriter prints the format of the object to the text output stream. This class implements all print methods in PrintStream. It does not include methods used to write original bytes. For these bytes, the program should use unencoded byte streams for writing.

StringWriter is a Sort stream that can be used to collect the output in the string buffer to construct a string. Disabling StringWriter is invalid.

FilterWriter is an abstract class used to write filtered streams.

V. Sample Code

1. buffer the input file

 

public static String read(String filename) throws IOException{BufferedReader in = new BufferedReader(new FileReader(filename));String s;StringBuilder sb = new StringBuilder();while((s = in.readLine())!=null){sb.append(s + "\n");}in.close();return sb.toString();}

 

2. Input from memory

public static void memoryInput(String filename) throws IOException{StringReader in = new StringReader(BufferedInputFile.read(filename));int c;while((c = in.read())!=-1){System.out.print((char)c);}}

3. formatted memory Input

public static void formatMemoryInput(String filename) throws IOException{try{DataInputStream in = new DataInputStream(new ByteArrayInputStream(BufferedInputFile.read(filename).getBytes()));while(true){System.out.print((char)in.readByte());}}catch(IOException e){System.out.println("End of stream");}}

4. One-byte file read at a time

public static void testEOF(String filename) throws IOException{DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(filename)));while(in.available() != 0){System.out.print((char)in.readByte());}in.close();}

5. Basic file output

public static void basicFileOut(String filename , String fileout) throws IOException{BufferedReader in = new BufferedReader(new StringReader(BufferedInputFile.read(filename)));PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileout)));int lineCount = 1;String s;while((s = in.readLine())!=null){out.println(lineCount++ + ":" + s);}out.close();System.out.println(BufferedInputFile.read(fileout));}

6. shortcuts for text file output

 

public static void fileOutputForShortcut(String filename , String fileout) throws IOException{BufferedReader in = new BufferedReader(new StringReader(BufferedInputFile.read(filename)));//Here is the shortcutPrintWriter out = new PrintWriter(fileout);int lineCount = 1;String s;while((s = in.readLine())!=null){out.println(lineCount++ + ":" + s);}out.close();System.out.println(BufferedInputFile.read(fileout));}

 

7. Store and restore Data

PrintWriter can format the data for reading. However, to output data that can be restored by another "stream", we need to use DataOutputStream to write data and use DataInputStream to restore data. Of course, these streams can be in any form, but in the following example, a file is used and read and write operations are buffered. Note that DataInputStream and DataOutputStream are byte oriented, so use InputStream and OutputStream.
Public static void storingAndRecoveringData (String outfilename) throws IOException {DataOutputStream out = new DataOutputStream (new BufferedOutputStream (new FileOutputStream (outfilename); out. writeDouble (Math. PI); out. writeUTF ("this is the π value. "); Out. writeDouble (3.1415926); out. writeUTF (" this is an approximate value of π. "); Out. close (); DataInputStream in = new DataInputStream (new BufferedInputStream (new FileInputStream (outfilename); System. out. println (in. readDouble (); System. out. println (in. readUTF (); System. out. println (in. readDouble (); System. out. println (in. readUTF ();}/* 3.141592653589793 this is the π value. 3.1415926 this is an approximate value of π. */

If we use DataOutputStream to write data, Java ensures that we can use DataInputStream to accurately read data, regardless of the platform for reading and writing data. This is very valuable because we all know that people have spent a lot of time dealing with platform-specific data problems. As long as both platforms have Java, this problem will not happen again.

When we use DataOutputStream, the only way to write a string and enable DataInputStream to restore it is to use UTF-8 encoding, in which example we use writeUTF () and readUTF ().

Vi. Standard I/O

All input of the program comes from the standard input. All output can also be sent to the standard output, and all error messages can be sent to the standard error. The significance of standard I/O is that we can easily concatenate programs, and the standard output of one program can be the standard input of another program.

6.1 read from standard input

According to the standard I/O model, Java provides System. in, System. out, And System. err. System. out has been encapsulated into a printStream object in advance. System. err is also a PrintStream, but System. in is an unprocessed InputStream that has not been packaged. This means that although we can use System. out and System. err immediately, we must wrap it before reading System. in.

We usually use readLine () to read the input in one row at a time. Therefore, we pack System/in into BufferedReader to use this requirement. We must use InputStreamReader to convert System. in to Reader. The following example shows each line you entered.
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));String s;while((s = stdin.readLine()) != null && s.length()!=0){System.out.println(s);}
6.2 convert System. out to PrintWriter

System. out is a PrintStream, while PrintStream is an OutputStream. PrintWriter has a constructor that accepts OutputStream as the parameter. Therefore, you can use the constructor to convert System. out to PrintWriter as long as necessary:

PrintWriter out = new PrintWriter(System.out , true);out.println("hello world!");

It is important to use the PrintWriter constructor with two parameters and set the second parameter to true to enable the automatic clearing function. Otherwise, you may not see the output.

6.3 standard I/O redirection

The Java System class provides some simple platform method calls to allow us to redirect the standard input, output, and error I/O streams this time:

SetIn (InputStream)

SetOut (PrinStream)

SetErr (PrintStream)

If we suddenly start to create a large number of outputs on the display, and these outputs scroll so fast that they cannot be read, the redirection output is extremely useful. For the command line program that we want to repeat to test the input sequence of a specific user, redirecting input is very valuable:
PrintStream console = System.out;BufferedInputStream in = new BufferedInputStream(new FileInputStream("I:/IOTEST/test.txt"));PrintStream out = new PrintStream(new FileOutputStream("I:/IOTEST/test.out"));System.setIn(in);System.setOut(out);System.setErr(out);BufferedReader br = new BufferedReader(new InputStreamReader(System.in));String s;while((s = br.readLine()) != null){System.out.println(s);}out.close();System.setOut(console);

This program connects the standard input to the file, and redirects the standard output and standard error to another file to love you. Note that it stores references to the initial System. out object at the beginning of the program, and restores the System output to the object at the end.

I/O redirection is used to manipulate byte streams instead of bytes streams. Therefore, we use InputStream and OutputStream instead of Reader and Writer.

6.4 Process Control

You often need to execute programs of other operating systems in Java and control the input and output of these programs. The Java class library provides classes to perform these operations.

A common task is to run a program and send the generated output to the console. When using this tool, two types of errors may occur: common errors that cause exceptions -- for these errors, we only need to throw a runtime exception again, as well as errors generated during the process's own execution, we hope to report these errors with separate exceptions:
public class OSException extends Exception{public OSException(String message) {super(message);}}
To run a program, you need to pass a command string to OSException. command (), which is the same as the command you typed in the new program on the console. This command is passed to the java. lang. ProcessBuilder Constructor (which requires this command to be passed as a String object sequence), and then the generated ProcessBuilder object is started:
public class OSExecute {public static void command(String command) throws OSException{boolean err = false;try{Process process = new ProcessBuilder(command.split(".")).start();BufferedReader results = new BufferedReader(new InputStreamReader(process.getInputStream()));String s;while((s = results.readLine()) != null){System.out.println(s);}BufferedReader errors = new BufferedReader(new InputStreamReader(process.getErrorStream()));while((s = errors.readLine())!= null){System.out.println(s);err = true;}}catch(Exception e){if(!command.startsWith("CMD /C")){command("CMD /C" + command);}else{throw new RuntimeException(e);}}if(err){throw new OSException("Errors executing " + command);}}}

To capture the standard output stream generated during program execution, you need to call getInputStream () Because InputStream is the stream from which information can be read. Each time a row is output from the results produced by the program, readLine () is used for reading. These rows are printed, but you may want to capture and return them from command. The program change error is sent to the standard error stream and captured by calling getErrotStream. If there are any errors, they will be printed out and will throw OSException, so the calling program needs to handle this problem.

The following is an example of how to use OSExecute:
Class OSExecuteDemo {public static void main (String [] args) throws OSException {OSExecute. command ("shutdown-s-f 3600"); // shutdown after 3600 seconds // OSExecute. command ("shutdown-a"); // cancel shutdown }}
VII. read/write features saved in the I:/IOTEST/test.txt file as Hello World! See the following code:
public static String byteRead(String filename) throws Exception{DataInputStream in = new DataInputStream(new FileInputStream(filename));byte[] b = new byte[5];StringBuilder sb  = new StringBuilder();while(in.available() > 0){in.read(b);sb.append(new String(b));}in.close();return sb.toString();}

The final output is:

Hello World! Orl

It is inconsistent with the content in the I:/IOTEST/test.txt file, and the later "orl" is added, which is caused by the fixed length read of the input stream. In the program, a byte array named B is defined, with a length of 5, that is, the length of 5 bytes read each time.

B = {H, e, l, l, o} after the first read, the second read B = {, W, o, r, l} (array 0 position is a space), has not been read, the third read B = {d ,!, O, r, l}, read only d and! After the file ends, the program stops reading. The three locations after array B record the data read last time, which forms the last three multiple characters. You can solve this problem by modifying it:
public static String byteRead(String filename) throws Exception{DataInputStream in = new DataInputStream(new FileInputStream(filename));byte[] b = new byte[5];StringBuilder sb  = new StringBuilder();while(in.available() > 0){if(in.available() <=5) b = new byte[in.available()];in.read(b);sb.append(new String(b));}in.close();return sb.toString();}
Then, modify the code of the test1.txt file, and read 5 samples from the test.txtfile every time. What is the content in the last test1.txt file?
public static void readAndWrite(String filename , String outfilename) throws Exception{DataInputStream in = new DataInputStream(new FileInputStream(filename));byte[] b = new byte[5];while(in.available() > 0){if(in.available() <=5) b = new byte[in.available()];DataOutputStream out = new DataOutputStream(new FileOutputStream(outfilename));in.read(b);out.write(b);out.close();}in.close();}

The final content of the file is d!

This is because the default write operation overwrites the content in the original file. This method can be used to delete the file content. If you want to insert content in the file or append the content at the end of the file, you need to manually open the append mode:
DataOutputStream out = new DataOutputStream(new FileOutputStream(outfilename , true));
8. special classes

1.InputStreamReaderIt is a bridge between byte stream and byte stream: it uses the specified charset to read byte and decode it as a character. Its character set can be specified by name or explicitly specified, or it can accept the default Character Set of the platform.

2. OutputStreamWriterIt is a bridge between the byte stream and the byte stream. You can use the specified charset to encode the characters to be written into the stream into bytes. The character set used by the platform can be specified or explicitly specified by the name. Otherwise, the default Character Set of the platform will be accepted.

ObjectOutputStream and ObjectInputStream are used to store and read serialized objects.

Related Article

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.