Comparison of read and write operations on files in JAVA
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 data, then use in .close () to close Enter flow. See Example 1 in full code. The common way to write data with fileOutputStream is: fileoutputstream out = new fileoutputstream (myfile.txt); PrintStream P = New PrintStream (out); write data with P.Println (), then use p.close () Close the input. 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 data, then use Br.Close () to close Cache, close the file with fr.close (). See Example 3 in full code. The common way to write files with filewriter is: FileWriter FW = New FileWriter (MyData.txt); PrintWriter Out = New PrintWriter (FW); write data in file with out.print or out.println, Out.print The only difference between OUT.PRINTLN is that the latter writes 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 DataInputStreamimport java.io *; class FileInputDemo {public static void main (String args []) {// args.length is equivalent to argc in Cif (args.length == 1. ) {try {// Open the file that is the first command line parameterFileInputStream 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 readwhile (in.available ()! = 0) {// print file line to screensystem.out.println (in.readline ());} in.close ();} catch (Exception E ) {System.err.println (File input error);}} elseSystem.out.println (Invalid parameters);}} Example 2: // FileOutputDemo // Demonstration of FileOutputStream and PrintStream classesimport java.io *; class FileOutputDemo {. Public static void main (string args []) {fileoutputstream out; // declare a file output ObjectPrintStream P; // declare a print stream objectTry {// connected to myfil e.txtout = new fileoutputstream (myfile.txt); // connect stream to the output streamp = 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 java.1.1 to read a fileimport 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 ;