Compare read-write operations in Java compared to files
Author: Jeru Liu
Date: November 29,2000
Version: 1.0
Java for the file to read and write operations of many examples, so that beginners feel very confused, I think it is necessary to carry out various methods
Analyze, classify, and clarify the similarities and differences between different methods.
A In JDK 1.0, it is common to read and write using the InputStream & OutputStream two base classes.
The FileInputStream in InputStream is similar to a file handle, which is used to manipulate the file, similar to the
We have fileoutputstream this object in OutputStream.
The common way to read data using FileInputStream is:
FileInputStream fstream = new FileInputStream (args[0]);
DataInputStream in = new DataInputStream (fstream);
Use In.readline () to get the data, and then close the input stream with In.close ().
See Example 1 for complete code.
The common way to write data using FileOutputStream is:
FileOutputStream out = new FileOutputStream ("MyFile.txt");
PrintStream p = new PrintStream (out);
Use P.PRINTLN () to write the data, and then close the input with P.close ().
See Example 2 for complete code.
Two In JDK 1.1, two new object Reader & Writer are supported, which can only be used to manipulate text files, and
InputStream & OutputStream in JDK1.1 can operate on either a text file or a binary file.
The common way to read files using FileReader is:
FileReader FR = new FileReader ("MyData.txt");
BufferedReader br = new BufferedReader (FR);
Use Br.readling () to read out the data, then close the cache with Br.close (), and close the file with Fr.close ().
See Example 3 for complete code.
The common way to write files using FileWriter is:
FileWriter FW = new FileWriter ("MyData.txt");
PrintWriter out = new PrintWriter (FW);
The only difference between Out.print and out.println is writing data in Out.print or out.println files.
Enter the data or it will automatically open a new line. After writing, remember to turn off the output with Out.close () and close the file with Fw.close ().
See Example 4 for complete code.
--------------------------------------------------------------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 {
The Open the file is the ' the ' ' the '
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
System.out.println (In.readline ());
}
In.close ();
catch (Exception e) {
SYSTEM.ERR.PRINTLN ("File input Error");
}
}
Else
System.out.println ("Invalid parameters");
}
}
Example 2:
Fileoutputdemo
Demonstration of FileOutputStream and PrintStream classes
Import java.io.*;
Class Fileoutputdemo
{
public static void Main (String args[]) {
FileOutputStream out; Declare a file output object
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 (The 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'll be wirte into the file!");
Out.close ();
Fw.close ();
catch (IOException e) {
System.out.println ("Uh Oh, got an IOException error!");
E.printstacktrace ();
}
}
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.