Comparison of file read/write operations in Java _ MySQL

Source: Internet
Author: User
Comparison of reading and writing operations on files in Java author: JeruLiu date: November29, 2000 version: 1.0 commemorating over one hundred points in chinaasp original article (comparison of reading and writing operations on files in Java) difficult to score, 555 ~~~, I don't know how the old demons who scored thousands of points flood the water. There are many examples of reading and writing files in Java, so that beginners can compare the reading and writing operations on files in Java.

Author: Jeru Liu
Date: November 29,2000
Version: 1.0

It's hard to score over one hundred points in chinaasp (A Comparison of file read/write operations in Java), 555 ~~~, I don't know how the old demons who scored thousands of points flood the water.

Many examples of reading and writing files in Java are confusing for beginners. I think it is necessary to perform various methods.
One analysis and classification to clarify the similarities and differences between different methods.

1. in JDK 1.0, the base classes InputStream and OutputStream are usually used for read/write operations.
In InputStream, FileInputStream is similar to a file handle, which is used to operate the file. Similarly, in
In OutputStream, we have the object FileOutputStream.


The common methods for reading data using FileInputStream are:
FileInputStream fstream = new FileInputStream (args [0]);
DataInputStream in = new DataInputStream (fstream );
Use in. readLine () to get data, and then use in. close () to close the input stream.
For the complete code, see Example 1.

A common method to write data using FileOutputStream is as follows:
FileOutputStream out = new FileOutputStream ("myfile.txt ");
PrintStream p = new PrintStream (out );
Use p. println () to write data, and then close the input with p. close.
For the complete code, see Example 2.


2. JDK 1.1 supports two new objects, Reader and Writer, which can only be used to operate text files.
InputStream & OutputStream in JDK can be used to operate text files or binary files.

Common methods for reading files using FileReader are:
FileReader fr = new FileReader ("mydata.txt ");
BufferedReader br = new BufferedReader (fr );
Use br. readLing () to read data, use br. close () to close the cache, and use fr. close () to close the file.
For the complete code, see Example 3.

The common methods for writing files using FileWriter are as follows:
FileWriter fw = new FileWriter ("mydata.txt ");
PrintWriter out = new PrintWriter (fw );
Write data into files using out. print or out. println. The only difference between out. print and out. println is that the latter writes
A new row is automatically opened when data is imported. Remember to close the output with out. close () and close the file with fw. close.
For the complete code, see Example 4.

------------------------------------------------------------ Following is the source code of examples ------------------------------------------------------

Example 1:
// FileInputDemo
// Demonstrates FileInputStream and DataInputStream
Import java. io .*;
PrintStream p; // declare a print stream object

Try {
// Connected to "myfile.txt"
Out = new FileOutputStream ("myfile.txt ");
// Connect print stream to the output stream
P = new PrintStream (out );
P. println ("This is written to a file ");
P. close ();
} Catch (Exception e ){
System. err. println ("Error writing to file ");
}
}
}

Example 3:
// FileReadTest. java
// User FileReader in JDK1.1 to read a file
Import java. io .*;

Class FileReadTest {
Public static void main (String [] args ){
FileReadTest t = new FileReadTest ();
T. readMyFile ();
}

Void readMyFile (){
String record = null;
Int recCount = 0;
Try {
FileReader fr = new FileReader ("mydata.txt ");
BufferedReader br = new BufferedReader (fr );
Record = new String ();
While (record = br. readLine ())! = Null ){
RecCount ++;
System. out. println (recCount + ":" + record );
}
Br. close ();
Fr. close ();
} Catch (IOException e ){
System. out. println ("Uh oh, got an IOException error! ");
E. printStackTrace ();
}
}

}

Example 4:
// FileWriteTest. java
// User FileWriter in JDK1.1 to writer a file
Import java. io .*;

Class FileWriteTest {
Public static void main (String [] args ){
FileWriteTest t = new FileWriteTest ();
T. WriteMyFile ();
}

Void WriteMyFile (){
Try {
FileWriter fw = new FileWriter ("mydata.txt ");
PrintWriter out = new PrintWriter (fw );
Out. print ("hi, this will be wirte into the file !");
Out. close ();
Fw. close ();
} Catch (IOException e ){
System. out. println ("Uh oh, got an IOException error! ");
E. printStackTrace ();
}
}

}
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.