Java SE learning _ Java read/write file _ IO

Source: Internet
Author: User

Detailed description of how to operate text files using Java
Abstract: At first, Java does not support text file processing. In order to make up for this defect, two classes, reader and writer, were introduced.
At first, Java does not support text file processing. In order to make up for this defect, two classes, reader and writer, are introduced. These two classes are abstract classes. In writer, write (char [] CH, int off, int length), flush () and close () are abstract methods, read (char [] CH, int off, int length) and close () in Reader () methods are abstract methods. Subclass should implement them separately.
When reading and writing text files, It is very convenient to use reader, such as filereader, inputstreamreader, and bufferedreader. The most important class is inputstreamreader, which serves as a bridge between byte conversion and character conversion. You can specify the encoding method in the constructor. If you do not specify the encoding method, the default encoding method of the underlying operating system will be used, for example, GBK (our Chinese Windows default. When using filereader to read files.

It is like inserting a pipe into it to start reading or writing data. This is a byte stream (input/output streamreader), and the pipe is further embedded into the pipe and converted into a bytes stream, making reading easier.
Filereader Fr = new filereader ("ming.txt ");
Int CH = 0;
While (CH = Fr. Read ())! =-1 ){
System. Out. Print (char) CH );
}
The read () method returns the read to get the next character. Of course, you can also use read (char [] CH, int off, int length), which is similar to processing binary files. If you use inputstreamreader to read files
While (CH = ISR. Read ())! =-1 ){
System. Out. Print (char) CH );
}
This is no different from filereader. In fact, the methods in filereader are inherited from inputstreamreader. The read () method is time-consuming. If we want to improve the efficiency, we can use bufferedreader to package the reader and protect our hard disk (buffer) in this way, the read speed can be improved. We can read text in one row and use the Readline () method.
Bufferedreader BR = new bufferedreader (New inputstreamreader (New fileinputstream ("ming.txt ")));
String data = NULL;
While (Data = Br. Readline ())! = NULL ){
System. Out. println (data );
}
When you understand how to use reader to read text files, it is also very easy to use writer to write files. Note that when writing a file, the written data is first put into the buffer zone and then written into the file to improve efficiency. So sometimes you need to actively call the flush () method. The method for writing files corresponding to the above is:
Filewriter fw = new filewriter ("hello.txt ");
String S = "Hello World ";
FW. Write (S, 0, S. Length (); // writes the sting type to s, from the first character of S to the last character.
FW. Flush ();
Outputstreamwriter OSW = new outputstreamwriter (New fileoutputstream ("hello2.txt "));
OSW. Write (S, 0, S. Length ());
OSW. Flush ();
Printwriter PW = new printwriter (New outputstreamwriter (New fileoutputstream ("hello3.txt"), true );

// True is added based on the original file. If true is not added, the original content will be overwritten.
PW. println (s );
Don't forget to close the stream after it is used up! The following is a small example to help beginners understand it. In fact, sometimes the Java I/O system requires us to remember more, or it will become unfamiliar someday.

Import java. Io .*;
Public class testfile2 {
Public static void main (string [] ARGs) throws ioexception {
Filereader Fr = new filereader ("ming.txt ");
Char [] buffer = new char [1024];
Int CH = 0;
While (CH = Fr. Read ())! =-1 ){
System. Out. Print (char) CH );
}

Inputstreamreader ISR = new inputstreamreader (New fileinputstream ("ming.txt "));
While (CH = ISR. Read ())! =-1 ){
System. Out. Print (char) CH );
}
Bufferedreader BR = new bufferedreader (New inputstreamreader (New fileinputstream ("ming.txt ")));
String data = NULL;
While (Data = Br. Readline ())! = NULL ){
System. Out. println (data );
}
Filewriter fw = new filewriter ("hello.txt ");
String S = "Hello World ";
FW. Write (S, 0, S. Length ());
FW. Flush ();
Outputstreamwriter OSW = new outputstreamwriter (New fileoutputstream ("hello2.txt "));
OSW. Write (S, 0, S. Length ());
OSW. Flush ();
Printwriter PW = new printwriter (New outputstreamwriter (New fileoutputstream ("hello3.txt"), true );
PW. println (s );
Fr. Close ();
ISR. Close ();
BR. Close ();
FW. Close ();
OSW. Close ();
PW. Close ();
}
}


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.