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, except FF, intlength), flush () and close () are abstract methods, and read (char [] ch, 0000ff, intlength) and close () methods in Reader are abstract methods. In the beginning, java does not support text file processing. 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, and read (char [] ch, int off, int length) and close () 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
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.
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 ();
}
}
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.