How to use Java to operate notepad

Source: Internet
Author: User


Use Java to operate notepad

At first, Java does not support text file processing. In order to make up for this defect, two classes, reader and writer, are introduced. These two classes are abstract classes. In writer, write (char [] CH, int off, int length), flush () and close () are abstract methods, read (char [] CH, int off, int length) and close () in Reader () methods are abstract methods.

When reading and writing text files, It is very convenient to use reader, such as filereader, inputstreamreader, and bufferedreader. The most important class is inputstreamreader, which serves as a bridge between byte conversion and character conversion. You can specify the encoding method in the constructor. If
If this parameter is not specified, the system uses the default encoding method of the underlying operating system, such as GBK. The following code reads a file using filereader:

 
Filereader Fr = new filereader ("C: // tjcyjd.txt ");
Int CH = 0;
While (CH = Fr. Read ())! =-1 ){
System. Out. Print (char) CH );
}

 
The read () method returns the read to get the next character. Of course, you can also use read (char [] CH, int off, int length), which is similar to processing binary files. If you use inputstreamreader to read files, the Code is as follows:

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. To improve the efficiency, we can use bufferedreader to package the reader, which can improve the read speed. We can read the text in one row, the code for using Readline () is as follows:

Bufferedreader
BR =
New bufferedreader (New inputstreamreader
(New fileinputstream ("F: // tjcyjd.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 use writer to write files. Note that when writing a file, the written data is first put into the buffer zone and then written into the file to improve efficiency. So sometimes you need to actively call the flush () method. The method for writing files corresponding to the above is:

Filewriter
FW = new filewriter ("F: // tjcyjd.txt ");
String S = "Hello World ";
FW. Write (S, 0, S. Length ());
FW. Flush ();
Outputstreamwriter OSW = new
Outputstreamwriter (New fileoutputstream
("Tjcyjd2.txt "));
OSW. Write (S, 0, S. Length ());
OSW. Flush ();
Printwriter PW = new printwriter
(New outputstreamwriter (New fileoutputstream
("Tjcyjd3.txt"), true );
PW. println (s );

 
Do not forget to close the stream after it is used up! I will post some code below, hoping to provide a reference when using it.


package test;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class TestFile{
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader ("F: / / tjcyjd. TXT"); / / content: love lives, love java! Love life, love java!
Int ch = 0;
while ((ch = fr.read()) != -1) {
System.out.print((char) ch);
}
InputStreamReader isr = new InputStreamReader(new FileInputStream(
"F://tjcyjd.txt"));
while ((ch = isr.read()) != -1) {
System.out.print((char) ch);
}
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream("F://tjcyjd.txt")));
String data = null;
while ((data = br.readLine()) != null) {
System.out.println(data);
}
FileWriter fw = new FileWriter("F://tjcyjd1.txt");
String s = "love lives, love java! Love life, love java!";
fw.write(s, 0, s.length());
Fw.flush ();
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
"F://tjcyjd2.txt"));
osw.write(s, 0, s.length());
Osw.flush ();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(
new FileOutputStream("F://tjcyjd3.txt")), true);
pw.println(s);
Fr.close ();
Isr.close ();
Br.close ();
Fw.close ();
Osw.close ();
Pw.close ();
}
}



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.