Use Java to operate text files

xiaoxiao2021-03-06  54

Using the Java operation binary document describes how to use Java to process binary files, this article mainly tells how to use Java to process text files.

Initially Java does not support the processing of text files, in order to make up for this shortcomings, two classes are introduced, these two classes are abstract classes, Write in Writer (char [] ch, int OFF, INT LENGTH) , Flush () and close () methods are abstract methods, read (char [] ch, int off, int layth) and close () methods are abstract methods. Subcombs should be achieved separately.

When we read and write text files, it is very convenient to use Reader, such as FileReader, InputStreamReader, and BufferedReader. The most important classes are InputStreamReader, which is a bridge that is converted to characters. You can redeemed the encoding in the constructor, if not specified, the default encoding method of the underlying operating system, such as GBK, etc. When using FileReader reads files. FileReader fr = new fileReader ("ming.txt"); int CH = 0; while ((ch = fr.read ())! = - 1) {system.out.print ((char) ch);} where read () The method returns to read the next character. Of course, you can also use read (char [] ch, int off, intleth, which is similar to the processing binary, not much. If you use InputStreamReader to read the file when you read the file ((ch = isr.read ())! = - 1) {system.out.print ((char) ch);} This is nothing difference, in fact, The method in the FileReader is inherited from the InputStreamReader. The READ () method is more effective, and if we can use BufferedReader to pack Reader in order to improve efficiency, this can improve the read speed, we can read the text in a 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, use Writer write files is also very simple. One thing to pay attention to, when you write the file, in order to improve efficiency, the written data will first put into the buffer and write to the file. 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 () ; OutputStreamWriter osw = new OutputStreamWriter (new ( "hello2.txt") FileOutputStream); 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 I use it! Here is a small example to help novice understand. In fact, sometimes Java IO systems need us to remember, or it will be alienated. The content of the test file is ming.txt Welcome to J2ME Development Network 12345HELLO World I like Java LanguageImport Java.io. *;

转载请注明原文地址:https://www.9cbs.com/read-116494.html

New Post(0)