Home >Java >javaTutorial >Detailed explanation of IO basics in Java advanced tutorial
This article mainly introduces the IO basics of Java advanced tutorial. This is just a basic introduction to Java IO, including reading and writing operations. Friends in need can refer to
The most important computer The function is to process data. A useful computer language needs to have good IO capabilities so that unprocessed data flows into the program and processed data flows 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 with one or two lines of programs. In Java, programmers often need multiple levels of decoration to achieve file reading.
The benefit of relative complexity is the flexibility of IO. In Java, programmers can control the entire process of IO to design the best IO method. We'll see more below.
IO example
The following is the file file.txt I used for demonstration
Hello World! Hello Nerd!
Let’s first study an example of file reading:
import java.io.*; public class Test { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new FileReader("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 handler. Please refer to the Java Advanced Tutorial on Exception Handling
Decorator and Function Combination
The key to program IO is to create a BufferedReader objectbr:
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
In During the creation process, we first created a FileReader object. The function of this object is to read the byte stream from the file "file.txt" and convert it into a text stream. In Java, the standard text encoding is unicode. BufferedReader() receives the FileReader object, expands the functions of FileReader, and creates a new BufferedReader object. In addition to the above-mentioned file reading and conversion functions, this object also provides cached reading (buffered) functions. Finally, we can read the file line by line by calling the readLine() method on the br object.
(Cache reading is to open an area in the memory as a cache, which stores the text stream read by FileReader. When the cache content is read (such as the readLine() command), the cache will be loaded Subsequent text stream. )
BufferedReader() is a decorator that receives a primitive object and returns a decorated object with more complex functions. The benefit of decorators is that they can be used to decorate different objects. What we are modifying here is the text stream read from the file. Other text streams, such as standard input, network transmission streams, etc., can be modified by BufferedReader() to achieve cached reading.
The following figure shows how br works, with data flowing from bottom to top:
The above decoration process is related to the text flow in Linux The ideas are very similar. In Linux, we use a function-like approach to process and pass text streams. In Java, we use decorators. But their purposes are similar, which is to achieve modularization and free combination of functions.
More combinations
In fact, Java provides a wealth of decorators. FileReader combines the two steps of reading and conversion, and adopts commonly used default settings, such as unicode for encoding. We can use the combination of FileInputStream + InputStreamReader to replace FileReader, thus separating the two steps of reading bytes and converting, and having better control over the two processes.
(Of course, FileReader is more convenient to use. InputStreamReader converts FileInputStream into a Reader for processing unicode text)
Arrow representation Data flow direction
Stream reading and writing come from four base classes: InputStream, OutputStream, Reader and Writer. InputStream and Reader handle reading operations, and OutputStream and Writer handle writing operations. They are both located in the java.io package. The inheritance relationship is as follows:
java.io
In addition, IOException has the following derived classes:
IOException
Reader and Writer and their derived classes handle unicode text. As we see Buffered Reader, InputStreamReader or FileReader.
InputStream and OutputStream and their derived classes handle byte streams. The data in the computer can be considered in byte form, so InputStream and OutputStream can be used to process a wider range of data. For example, we can use the following combination to read the data (such as integers) contained in the compressed file:
箭头表示数据流动方向
我们从压缩文件中读出字节流,然后解压缩,最终读出数据。
写入
写入(write)操作与读取操作相似。我们可以通过使用装饰,实现复杂的写入功能。这里是一个简单的写入文本的例子:
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"); } } }
上面创建了file对象,用于处理文件路径。
总结
这里只是对Java IO的基本介绍。Java的IO相对比较复杂。Java程序员需要花一些时间来熟悉java.io中的类及其功能。
The above is the detailed content of Detailed explanation of IO basics in Java advanced tutorial. For more information, please follow other related articles on the PHP Chinese website!