Comparison of read/write operations on files in Java

Source: Internet
Author: User

Comparison of read/write 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 .*;

Class FileInputDemo {
Public static void main (String args []) {
// Args. length is equivalent to argc in C
If (args. length = 1 ){
Try {
// Open the file that is the first command line parameter
FileInputStream fstream = new FileInputStream (args [0]);
// Convert our input stream to a DataInputStream
DataInputStream in = new DataInputStream (fstream );
// Continue to read lines while there are still some left to read
While (in. available ()! = 0 ){
// Print file line to screen

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.