Use Java to read the content in a text file

Source: Internet
Author: User

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 underlying operating system uses the default encoding method, such as GBK. When using filereader to read files.
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. To improve the efficiency, we can use bufferedreader to package the reader. In this way, the read speed can be improved. We can read the text in one row, 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 ());
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 );
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. The content of the test file is
Ming.txt
Welcome to Java Chinese site 12345

Hello world I like Java language
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.