Java text File Operation Method Example detailed _java

Source: Internet
Author: User
Tags ming

This example describes how to manipulate Java text files. Share to everyone for your reference. The specific analysis is as follows:

Originally Java did not support the processing of text files, in order to compensate for this shortcoming introduced the reader and writer two classes, these two classes are abstract classes, writer write (char[] ch,int off,int length), flush () and the Close () method is an abstract method, the read (char[] ch,int off,int length) and the Close () method in reader are abstract methods. Subclasses should implement them separately.

When we read and write text files, it is very convenient to use reader, such as Filereader,inputstreamreader and BufferedReader. One of the most important classes is InputStreamReader, which is a bridge between bytes converted to characters. You can specify the encoding in the constructor, and the default encoding of the underlying operating system, such as GBK, will be used if unspecified. When you use FileReader to read a file,

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


where the Read () method returns the next character read. Of course you can also use the Read (char[] ch,int off,int length) This is similar to the processing of binary files, not to say more. If you use InputStreamReader to read a file

while (ch = isr.read ())!=-1)
{
  System.out.print ((char) ch);


There is no difference between this and filereader, and in fact the methods in FileReader are inherited from the InputStreamReader. Read () method is relatively good time, if in order to improve efficiency we can use BufferedReader to packaging reader, so as to improve the speed of reading, we can read the text on one 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);
}

When you understand how to use reader to read text files, it is also very easy to write files with writer. It is important to note that when you write a file, the data that is written is put into the buffer and then written to the file for efficiency. So sometimes you need to actively invoke the flush () method. The corresponding method for writing the file 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 ("FileOutputStream"), true);
Pw.println (s);

Do not forget to close the stream after use! Here is a small example to help beginners understand. In fact, sometimes the Java IO system is a need for us to remember more, otherwise the day will be unfamiliar.

Hello world I like Java language import java.io.*; public class TestFile2 {public static void main (string[] args) throws IOException {FileReader fr = new Fileread
  ER ("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 ("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 ("FileOutputStream"), True);
  Pw.println (s);
  Fr.close ();
  Isr.close ();
  Br.close ();
  Fw.close ();
  Osw.close ();
  Pw.close ();

 }
}

I hope this article will help you with your Java programming.

Related Article

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.