The most important function of a computer is to process data. A useful computer language needs to have good IO capabilities to let the unhandled data flow into the program and let the processed data flow out.
Java's IO functionality is complex compared to other languages. In other languages, many IO functions (such as reading files) are encapsulated and can be implemented in one or two-line programs. In Java, programmers often need multiple levels of decoration (decoration) to implement file reads.
The benefits of relative complexity are the flexibility of IO. In Java, programmers can control the entire process of IO, thus designing the best Io method. We will see more below.
IO Example
Here is the file I used for the demo file.txt
Hello world! Hello nerd!
Let's start with an example of a file read:
Import java.io.*;PublicClasstest{PublicStaticvoid Main (string[] args) {try {
bufferedreader br =
new bufferedreader (new filereader ("file.txt" Span style= "color: #000000"));
String line = Br.readline (); while (line! = null Br.readline ();}
br.close ();} catch (IOException e) {System.out.println ("IO problem") Span style= "color: #000000"); }}}
This program contains an try...catch...finally exception handler. Refer to Java Advanced 02 exception handling
decorators and functional combinations
The key to program IO is to create a BufferedReader object br:
BufferedReader br = new BufferedReader (New FileReader ("file.txt"));
In the process of creation, we first set up a FileReader object that reads the byte stream from the file "file.txt" and transforms it into a text stream. In Java, the standard text encoding is Unicode. BufferedReader () receives the FileReader object and expands the functionality of the FileReader to create a new BufferedReader object. In addition to the above functions of file read and conversion, this object also provides the function of cache read (buffered). Finally, we can read the file row by line by calling the ReadLine () method on the BR object.
(a cache read is an area in memory that is used as a cache, which holds the stream of text that FileReader reads.) When the cached content is read away (such as the ReadLine () command), the cache loads the subsequent text stream. )
BufferedReader () is an adorner (decorator) that receives an original object and returns a decorated, functionally more complex object. The advantage of decorators is that it can be used to decorate different objects. What we are modifying here is the stream of text that is read from the file. Other text streams, such as standard input, stream streams, and so on, can be modified by BufferedReader () to enable cache reads.
Shows how BR works, with data flowing from bottom up:
(text stream can refer to Linux text Stream and TCP protocol and stream communication)
The above decoration process is similar to the idea of text flow in Linux. In Linux, we use similar functions to process and pass text streams. In Java, we used adorners. But their purpose is similar, is to achieve the function of the modular and free combination.
more Combinations
In fact, Java offers a rich decorator. The FileReader incorporates read and transform two steps, and uses common default settings, such as encoding to take Unicode. We can use a combination of FileInputStream + InputStreamReader to replace the FileReader, thus separating the read byte and converting two steps and having better control over the two processes.
(Of course, the use of FileReader is more convenient.) InputStreamReader is converting FileInputStream to a reader for processing Unicode text)
Arrows indicate data flow direction
Stream reads and writes from four base classes: InputStream, outputstream, Reader, and Writer. InputStream and reader are handling read operations, and OutputStream and writer are processing writes. They are all located in the Java.io package. The inheritance relationship is as follows:
java.io
In addition, IOException has the following derivative classes:
IOException
Reader and writer and their derived classes are processing Unicode text. As we see in buffered Reader, InputStreamReader or FileReader.
InputStream and OutputStream and their derived classes are processing byte (byte) streams. Data in a computer can be thought of as a byte form, so InputStream and outputstream can be used to handle more extensive data. For example, we can use the following combination to read the data contained in the compressed file (such as integers):
Arrows indicate data flow direction
We read out the byte stream from the compressed file, then unzip it, and finally read the data.
Write
Write operations are similar to read operations. We can implement complex write functions by using decorations. Here is a simple example of writing the text:
Import java.io.*;PublicClasstest{PublicStaticvoid main (string[] args) {try; 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") Span style= "color: #000000"); } }}
The file object is created above to handle the path to the files.
Java Basic Learning--21, IO basics