The example of this article tells the operation method of the Java text file. Share it for everyone for your reference. The specific analysis is as follows:
At first Java did not support the processing of text files. In order to make up for this shortage, the two classes of Reader and Writer were introduced. These two categories are abstract categories. Write (char [] ch, intnted, int laonGth) in Writer) , FLUSH () and Close () are abstract methods. Read (char [] ch, int lay, int length) and close () methods in the reader are abstract methods. The subclasses should be realized separately.
When we read and write text files, it is very convenient to use Reader, such as FileReader, InputStreamReader and BufferEdream. The most important class is InputStreamReader, which is a bridge converted into characters by bytes. You can specify the coding method in the constructor. If you do not specify, you will use the default encoding method of the underlying operating system, such as GBK. When reading files with FileReader,
FileReader FR = New Filereader ("Ming.txt"); int ch = 0; while ((ch = fr.read ())! =-1) {system.out.print ((char) ch);}Among them, the read () method returns to read the next character. Of course, you can also use Read (Char [] ch, int OFF, Int Langth). This is similar to when processing binary files, not much to say. 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 better for time. If we can use the bufferedReader to pack the efficiency, we can pack the reader. This can improve the speed of reading. We can read text and use the Readline () method.
BufferedReader Br = New BufferedReader {System.out.println (data) ;}When you understand how to read the text file with Reader, it is also very simple to write files with writer. One thing to note is that when you write files, in order to improve the efficiency, the written data will be placed in the buffer first, and then the file is written. So sometimes you need to actively call the FLUSH () method. The method of writing files above is::
Filewriter fw = new filewriter ("Hello.txt"); string s = "Hello World"; fw.write (s, 0, s.Length ()); fw.flush (); ReamWriter (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 flow after use! Here are a small example to help novices understand. In fact, sometimes Java's IO system requires us to remember more, otherwise it will be sparse someday.
// Hello World I like Java Languageimport Java.io.*; Public Class Testfile2 {Public Static Void Main (String [] ARGS) Thrown IOEXCEPTION New Filereader ("Ming.txt"); Char [] buffer = new char [1024]; int ch = 0; about ((ch = Fr.Read ())! =-1) {system.out.print ((char) ch);} InputStreamReader isr = New InputStreamReader " ming.txt "); space Stream ("" ming.txt "))); string data = null; while ((data = br.readline ()))! = Null) {system.out.println (data);} Filewriter Fw = New Filewriter (" Hello.txt " );; Fw.write (s, 0, s.Length ())); fw.flush (); outputStreamwriter OSW = New OutputStreamwriter (New FileoutStream xt ")); OSW. Write (s, 0, s.Length ()); Osw.flush (); Printwriter Pw = New Printwriter (new outputStreamwriter (new fileoutputstream ("Hello3.txt"); True); pw.prin tln (s); FR .close (); isr.Close (); Br.Close (); fw.close (); osw.close (); pw.close ();}}}}It is hoped that this article is helpful to everyone's Java program design.