tag: blog Java OS file IO Re
Import Java. io. bufferedreader; import Java. io. bufferedwriter; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. filereader; import Java. io. filewriter; import Java. io. ioexception; import Java. io. linenumberreader; import Java. NIO. buffer; public class Io {public static void main (string [] ARGs) throws ioexception {writefile (); readfile1 (); readfile2 ();} // Write Public static void writefile () Throws ioexception {fileoutputstream Fos = new fileoutputstream ("fos.txt"); FOS. write ("ABCD ". getbytes (); FOS. close () ;}// read public static void readfile1 () throws ioexception {fileinputstream fis1 = new fileinputstream ("fos.txt"); int CH = 0; while (CH = fis1.read ())! =-1) // read {system. out. println (char) CH);} fis1.close ();} public static void readfile2 () throws ioexception {fileinputstream fis2 = new fileinputstream ("fos.txt "); byte [] Buf = new byte [1024]; // buffer int Len = 0; while (LEN = fis2.read (BUF ))! =-1) // read the returned value in array size is the number of reads {system. out. println (new string (BUF, 0, Len);} fis2.close (); fileinputstream fis3 = new fileinputstream ("fos.txt "); // number of bytes in the fis2.available file int num = fis3.available (); // defines a buffer that is just right, memory byte [] buf2 = new byte [fis3.available ()]; fis3.read (buf2); system. out. println (Num + ":" + new string (buf2); fis3.close ();}}