This is the first test example of my first file file, divided into two parts: myfirstiofile and myfirstrafile1. MyfirstiofileImport java.io. *;
class MyFirstIOFile {public static void main (String [] args) {char ch = ''; System.out.println ( "plz input some characters."); try {FileOutputStream my_outfile = new FileOutputStream ( "..// myfile. TXT "); while ((CH = (char) system.in.read ())! = '#') {my_outfile.write ();} my_outfile.close ();} catch (ooException e) {};
System.out.println ("Show the file characters:"); try {fileinputstream my_infile = new fileinputstream ("..// myfile.txt"); // Byte [] k = {}; error: this so-called dynamic Assign bytes that cannot be read in the file. Byte [] k = new byte [20]; my_infile.read (k); System.out.write (k); my_infile.close ();} catch (filenotfoundexception e) {} catch (ooException e) {}; }2. MyfirstraFile // Due to two cases of randomaccessfile (String / File, String Mode): RW (write) and R (read) // Since write or read in the RandomaccessFile object, it is necessary to distinguish it. Come, you must define two different objects! // The following MY_RAFILE and MY_RAFILE_R can accurately use the RandomaccessFile objects to bring the write and read methods.
Import java.io. *; // Import java.lang. *; // import java.util. *;
Class myfirstrafile {public static void main (string [] args) {// string test = new string ("longtest"); // long ml = long.parselopelong ("EWT"); // long my_long = new long (Test " ); // long my_long = my_long.longvalue (); // string ms = my_long.toString (); //system.out.println (MY_LONG); randomaccessfile my_raftfile = null; try {my_rafile = new randomaccessfile (".. //myfile.txt", "RW"); for (int i = 0; i <10; i ) my_rafile.writebytes ("RandomaccessFile Test Line: i " / r / n "); // my_rafile. Close ();} catch (filenotfoundexception e) {} catch (ioException e) {}; try {// my_rafile = new randomaccessfile ("..// myfile.txt", "rw"); my_rafile.seek (my_rafile. length ()); my_rafile.writeBytes ( "append line / r / n"); my_rafile.close ();} catch (FileNotFoundException e) {} catch (IOException e) {}; try {RandomAccessFile my_rafile_r = new RandomAccessFile ( " ../MYFILE.TXT ""); string record = ""; INT i = 0; while ((record = my_rafile_r.readline ())! = null) { I ; system.out.println ("Value" i ":" Record);} my_rafile_r.close ();} catch (ooexception e) {};
} // main} // Class