Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!
The most important function of a computer is to process data. A useful computer language requires good I/O functions to allow the flow of unprocessed dataProgramTo let the processed data flow out.
Compared with other languages, Java's Io functions are complex. In other languages, many Io functions (such as reading files) are encapsulated and can be implemented using one or two lines of programs. In Java, programmers often need multiple layersDecoration(Decoration)To read files.
The advantage of relative complexity is the flexibility of Io. In Java, programmers can control the entire Io process to design the best Io method. We will see more below.
Io example
The following is the demo file file.txt.
Hello world! Hello nerd!
Let's first look at an example of file reading:
Import Java. Io .* ; Public Class Test { Public Static Void Main (string [] ARGs) { Try {
Bufferedreader br=
NewBufferedreader (NewFilereader ("file.txt"));
String line = BR. Readline (); While (Line! = Null ) {System. Out. println (line); line = BR. Readline ();}
BR. Close ();} Catch (Ioexception e) {system. Out. println ( "Io problem" );} }}
This program contains a try... catch... finally exception processor. Refer to Java advanced 02 for exception handling.
Decorator and function combination
The key to program Io is to create the bufferedreader object BR:
Bufferedreader BR = new bufferedreader (New filereader ("file.txt "));
In the creation process, we first establishFilereaderObject. This object is used to read byte streams from the file "file.txt" and convert them to text streams. In Java, the standard text encoding method is Unicode. Bufferedreader () receives the filereader object, expands the filereader function, and createsBufferedreaderObject. In additionFile ReadingAndConversionIn addition toCache read(Buffered). Finally, we can call the Readline () method on the BR object to read files row by row.
(Cache reading is to open up a region in the memory as the cache, which stores the text stream read by filereader. After the cached content is read (such as the Readline () Command), the cache loads subsequent text streams .)
Bufferedreader () isDecorator(Decorator), which receives an original object and returns a decorated object with more complex functions. The benefit of modifier is that it can be used to modify different objects. Here we are modifying the text stream read from the file. Other text streams, such as standard input and network-transmitted streams, can be modified by bufferedreader () to implement cache reading.
Shows the working method of the BR, and the data flows from the bottom up:
(For details about text stream, refer to Linux text stream and TCP protocol and stream communication)
The above decoration process is similar to the text stream idea in Linux. In Linux, we use functions like this to process and transmit text streams. In Java, we use the decorator. However, their goals are similar, that is, the modular and free combination of functions.
More combinations
In fact, Java provides a variety of decorators. Filereader combines the read and conversion steps and uses common default settings, such as Unicode encoding. We can useFileinputstream+InputstreamreaderTo replace filereader.SeparationRead byte and convert, and have better control over the two processes.
(Of course, filereader is more convenient to use. Inputstreamreader converts fileinputstream into a reader to process Unicode text)
Arrows indicate the direction of Data Flow
Stream read/write comes from four base classes:Inputstream,Outputstream,ReaderAndWriter. Inputstream and reader are read operations, while outputstream and writer are write operations. They are all located in the Java. Io package. The inheritance relationship is as follows:
Java. Io
In addition, ioexception has the following types:
Ioexception
Reader, writer, and Other types process Unicode text. See buffered reader, inputstreamreader, or filereader.
Inputstream, outputstream, and their bytes class are used to process byte streams. Data in the computer can be considered as bytes, so inputstream and outputstream can be used to process more extensive data. For example, we can use the following combination to read the data contained in the compressed file (such as an integer ):
Arrows indicate the direction of Data Flow
We read the byte stream from the compressed file, decompress it, and finally read the data.
Write
Write operations are similar to read operations. We can use decoration to implement complex write functions. Here is a simple example of writing text:
Import Java. Io .* ; Public Class Test { Public Static Void Main (string [] ARGs ){ Try {String content = "Thank you for your fish ." ; File File = New File ("new.txt" ); // Create the file if doesn' t exists If (! File. exists () {file. createnewfile ();} filewriter FW = New Filewriter (file. getabsolutefile (); bufferedwriter BW = New Bufferedwriter (FW); BW. Write (content); BW. Close ();} Catch (Ioexception e) {system. Out. println ( "Io problem" );}}}
The file object is created above to process the file path.
Summary
This is just a basic introduction to Java Io. Java Io is relatively complicated. Java Programmers need to spend some time familiarizing themselves with the classes and functions in Java. Io.
Welcome to continue reading the "Java quick tutorial" SeriesArticle