Java Advanced IO Basics (reprint)

Source: Internet
Author: User

IO Example

The following is a presentation of the file file.txt

Hello world! Hello nerd!
Let's look at an example of a file read:

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");
}
}
}

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 FileReader function 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. 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)

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.

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.

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.*;
public class test{
public static void Main (string[] args) {
try{
String content = "Thank for your fish.";
File File = new file ("New.txt");
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 handle the path to the files.

Java Advanced IO Basics (reprint)

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.