Several methods of reading files
Import Java. io. bufferedreader; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. filereader; import Java. io. filewriter; import Java. io. stringreader; import Java. io. stringwriter;/*** the file on the disk must exist ** @ author brother Huan **/public class fileread {public fileinputstream FS; // public fileoutputstream FOS in bytes; // In bytes public filereader fr; // in characters public filew Riter FW; // The unit of characters is public stringreader SR; Public stringwriter SW; Public fileread () {file = new file ("D: // test.txt "); try {system. out. println ("reads the file content in bytes, one byte at a time:"); FD = new fileinputstream (File); int temp; system. out. println ("read content --" "); While (temp = Fi. read ())! =-1) {system. out. print (temp + ",");} FCM. close (); system. out. println (""); //************************************** * ********************** // system. out. println ("Read File Content in bytes, read multiple bytes at a time:"); byte [] B = new byte [11]; FD = new fileinputstream (File ); while (FCM. read (B )! =-1) {system. out. println ("B length ---" "+ B. length);} fiis. close (); system. out. println (""); //************************************** * ********************** // system. out. println ("reads the file content in characters, one character at a time:"); Fr = new filereader (File); int A; while (A = Fr. read ())! =-1) {system. out. print (a + ",");} Fr. close (); system. out. println (""); //************************************** * ********************** // system. out. println ("Read File Content in characters, multiple characters at a time:"); Fr = new filereader (File); char [] C = new char [100]; while (Fr. read (c )! =-1) {for (char M: C) {system. out. print (m + ",");} system. out. println ("");} system. out. println (""); //************************************** * *********** system. out. println ("read in behavior units"); bufferedreader BR = new bufferedreader (New filereader (File); string s; while (S = BR. readline ())! = NULL) {system. Out. println (s) ;}} catch (exception e) {e. printstacktrace ();}}}
Write operations on files.
Import Java. io. bufferedreader; import Java. io. bufferedwriter; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. filereader; import Java. io. filewriter; import Java. io. ioexception; import Java. io. stringreader; import Java. io. stringwriter; import Java. NIO. charbuffer; public class filewrite {public fileinputstream FCM; Public fileoutputstream Fos; Public filereader Fr; Public filewriter FW; Public stringreader SR; Public stringwriter SW;/*** test. TXT files are written to test1.txt */Public filewrite () {file file1 = new file ("D: // test.txt"); file file2 = new file ("D: // test1.txt "); if (file2.exists () {file2.delete () ;}try {file2.createnewfile (); Fos = new fileoutputstream (file2); FD = new fileinputstream (file1 ); FW = new filewriter (file2); Fr = new filereader (file1 ); //************************************** * *********** int B; while (B = FCM. read ())! =-1) {// a single byte FOS. write (B );} //************************************** * *********** byte [] b1 = new byte [512]; while (FCM. read (B1 )! =-1) {// multiple bytes of Fos. write (B1 );} //************************************** * *********** char [] C1 = new char [100]; int C; while (C = Fr. read (C1 ))! =-1) {// multiple characters. You can think of a single character. write (c) ;}} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}
Note: Multiple bytes are much more efficient than a single byte.