Java has a lot of examples of reading and writing, so that beginners feel very confused, I think it is necessary to analyze, classify, classify, and clarify the difference between different methods.
One. In JDK 1.0, it is usually read or written in both base classes of InputStream & OutputStream. FileInputStream in InputStream is similar to a file handle, which is operated by it, similar, in OutputStream, we have fileOutputStream.
The common way to read data with fileInputStream is: fileInputstream fstream = new fileInputstream (args [0]); DataInputStream IN = New DataInputStream (fstream);
Use IN.Readline () to get the data, then close the input stream with in .close (). See Example 1 in full code.
The common way to write data with FileOutputStream is: fileoutputstream out out = new fileoutputstream (myfile.txt); PrintStream P = New PrintStream (OUT);
Write data with P.Println () and then close your input with P.Close (). The full code is shown in Example 2.
two. In JDK 1.1, support two new object Reader & Writer, which can only be used to operate text files, while INPUTSTREAM & OUTPUTSTREAM in JDK1.1 can operate text files or binary files.
The common way to read files with FileReader is: FileReader fr = new fileReader (MyData.txt); BufferedReader Br = New BufferedReader (fr);
Use br.readling () to read the data, then use Br.Close () to turn off the cache and close the file with fr.close (). See Example 3 in full code.
The common way to write files with FileWrit is: FileWriter FW = New FileWriter (MyData.txt); PrintWriter Out = New PrintWriter (fw);
The only difference between output and out.println is written to the data in the file with out.print or out.println, which is written to data or automatically opens a new line. After writing, remember to turn the output with out.close (), shut down the file with fw.close (). See Example 4 in full code.
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 what is the first command line parameter
FileInputStream FStream = New FileInputStream (Args [0]);
// Convert Our Input Stream to a DataInputStreamDataInputStream in = New DataInputStream (FStream);
// Continue to read Lines While there is still some limited 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 {
// connect to myfile.txt
OUT = New fileoutputStream (myfile.txt);
// Connect Print Street 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 rord = null;
INT Recount = 0;
Try {
FileReader fr = new fileReader (MyData.txt);
BufferedReader Br = New BufferedReader (FR);
Record = new string ();
While ((record = br.readline ())! = null) {
Recount ;
System.out.println (Recount : Record);
}
br.close ();
Fr.close ();
} catch (ioexception e) {
System.out.println (uh oh, got an oException error!);
E.PrintStackTrace ();
}
}
}
EXAMPLE 4:
// FileWritTest.java
// User FileWriter in JDK1.1 To Writer a fileimport java.io. *;
Class FileWriteTest {
Public static void main (String [] args) {
FileWrittest 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 t.");
Out.close ();
Fw.close ();
} catch (ioexception e) {
System.out.println (uh oh, got an oException error!);
E.PrintStackTrace ();
}
}
}