Java text file read/write

Source: Internet
Author: User

The read () and Close () methods in reader are abstract methods, and the write (), flush (), and Close () methods are abstract methods in the Java IO system, which uses reader and writer two abstract classes. Subclasses should implement them separately.

Javaio has provided us with three easy-to-implement classes for reader, FileReader, InputStreamReader, and Bufferreader. The most important of these is the InputStreamReader class, which is a bridge that converts bytes into characters. You can re-construct the encoding, if not specified, using the default encoding of the underlying operating system, such as GBK.

First give an example of filereader read txt, very simple.

New FileReader ("c:/test.txt"); int 0 ;  while ((Ch=fr.read ())!=-1) {     System. out. println ((char) ch);}

Where the Read method returns the next character read.

It is also very simple to give an example of inputstramreader reading txt.

1InputStream is=NewFileInputStream (NewFile (filepath));2InputStreamReader FR =NewInputStreamReader ( is);3 intCH =0;4  while((Ch=fr.read ())!=-1){5System. out. println (Char) ch);6}

Here can see this and filereader no difference, just need to use the other class objects more, can be simple to understand, I want to use is inputstreamreader this thing, so there will be inputstreamreader to let me Reader, So to create a inputstream, and this inputstream is to read the file, so the file read stream to read the file, the internal parameters of the file object.

In fact, the methods in FileReader are inherited from the InputStreamReader.

The read () method is time-consuming, and in order to improve efficiency, we can use BufferedReader to wrap the reader so that it increases the read speed and, in turn, reads the text in a row.

Such as:

1 New BufferedReader (new  FileReader (filepath)); 2 String data = Br.readline (); // read one line at a time until null is the end 3  while (data!=null) {4     System.  out . println (data); 5     data = br.readline (); 6 }

Note here that the last read is null when the end , the middle can have more than a simple line-wrapping without ending.

There is reading and there is writing. There are three ways to write text using printwriter,FileWriter , and bufferwriter .

The code is as follows

1FileWriter FW =NewFileWriter ("D:/test.txt"); 2String s ="Hello World"; 3Fw.write (s),0, S.length ()); 4 Fw.flush (); 5           6OutputStreamWriter OSW =NewOutputStreamWriter (NewFileOutputStream ("D:/test1.txt")); 7Osw.write (s),0, S.length ()); 8 Osw.flush (); 9           TenPrintWriter PW =NewPrintWriter (NewOutputStreamWriter (NewFileOutputStream ("D:/test2.txt")),true);  One Pw.println (s);  A            - Fw.close ();  - Osw.close ();  thePw.close ();

The above code is to write the file from scratch, first delete the original contents of the file, and then write the new data, if you want to continue to write the source file content, in the declaration of FileWriter FW, the parameters of the inside parameter plus a true on the line, namely

New FileWriter ("Test.txt",true);

Above is the example of FileWriter and PrintWriter, and then write a bufferwriter example,

1File File =NewFile ("D:/test.txt"); 2File dest =NewFile ("D:/new.txt"); 3 Try {  4BufferedReader reader =NewBufferedReader (Newfilereader (file)); 5BufferedWriter writer =NewBufferedWriter (NewFileWriter (dest)); 6String line =Reader.readline (); 7      while(line!=NULL){  8 Writer.write (line); 9line =Reader.readline (); Ten     }   One Writer.flush ();  A Reader.close ();  - Writer.close ();  -}Catch(FileNotFoundException e) { the E.printstacktrace ();  -}Catch(IOException e) { - E.printstacktrace ();  -}

The code is finished, and the following are their differences.

PrintWriter provides the print system method, in characters, to support Chinese characters.
BufferedWriter provides buffering to accelerate
FileWriter for writing files

The OutputStreamWriter is in bytes and does not support Chinese characters.

PrintWriter and BufferedWriter are all inherited java.io.Writer, so many functions are the same. However, PrintWriter provides the println () method to write line breaks for different platforms, and BufferedWriter can set the buffer size arbitrarily.

OutputStream can be passed directly to PrintWriter (BufferedWriter cannot receive), such as:

New PrintWriter ( new Bufferedoutputstream (new FileOutputStream ("Foo.out"));   

The PrintStream class is a non-negligible member of the filter class, and the most basic standard output is based on it-the System.out variable we commonly use is the PrintStream instance. The corresponding character stream class is the PrintWriter class.

The above is the basic file read and write operation content, need more practice and use in order to have a better understanding of it.

The code in this article is for your own hand knock test.

Reference the original URL: http://lapulande.iteye.com/blog/706051

Java text file read/write

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.