Example of Java text file manipulation method

  • 2020-04-01 03:55:14
  • OfStack

This article illustrates how to manipulate a Java text file. Share with you for your reference. Specific analysis is as follows:

To compensate for Java's initial lack of support for text file processing, two classes, Reader and Writer, were introduced. Both classes are abstract methods: write(char[] ch,int off,int length), flush() and close() in Writer are abstract methods; read(char[] ch,int off,int length) and close() in Reader are abstract methods. Subclasses should implement them separately.

Readers are handy when reading or writing text files, such as FileReader, InputStreamReader, and BufferedReader. The most important of these classes is InputStreamReader, which is a bridge between bytes and characters. You can respecify the encoding in the constructor, otherwise the default encoding of the underlying operating system, such as GBK, will be used. When reading files with FileReader,


FileReader fr = new FileReader("ming.txt");
int ch = 0;
while((ch = fr.read())!=-1 )
{
  System.out.print((char)ch);
}

Where the read() method returns a read to the next character. Of course, you can also use read(char[] ch,int off,int length), which is similar to binary files. If you use InputStreamReader to read the file


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, and if we want to improve efficiency by wrapping the Reader with a BufferedReader, we can read the text line by line, using 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);
}

Writing with Writer is also easy when you understand how to use Reader to read text files. One thing to note is that when you write a file, to be more efficient, the data you write is put into a buffer and then written to the file. So sometimes you need to actively call the flush() method. The corresponding method of writing files is as follows:


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 using it! Here is a small example to help beginners understand. In fact, sometimes Java IO system is we need to remember more, or which day is rusty.


//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();
  }
}

I hope this article has been helpful to your Java programming.


Related articles: