There are too many APIs related to I/O operations in Java, and for historical reasons, some APIs have been deprecated, sometimes confusing for some beginners, today, I took the time to sort out frequently used stream operations and share them out. If there are any mistakes, please correct them because most of the methods have been commented out, so the test code in main will not write comments.
For theoretical information, refer to NLP.
2008.07.03 was edited again and some new methods were added.
Package COM. syj. util; import Java. io. bufferedreader; import Java. io. bufferedwriter; import Java. io. bytearrayinputstream; import Java. io. bytearrayoutputstream; 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. inputstream; import Java. io. inputstreamreader; import Java. io. objectinpu Tstream; import Java. io. objectoutputstream; import Java. io. outputstream; import Java. io. printwriter; import Java. io. reader; import Java. io. stringreader; import Java. io. stringwriter; import Java. io. writer; import Java. util. arrays;/***** <p> * Title: Io tool class * </P> ** <p> * description: commonly used io operation package * </P> ** <p> * copyright: Reprinted please indicate the source of http://blog.csdn.net/sunyujia/ * </P> ** @ author Sun Jia * @ main sunyujia@yahoo.cn * @ Date Jun 15,200 8 4:37:58 */public class ioutil {/*** buffer size 1 MB */Private Static final int buffer_size = 1024*1024;/*** description: output the input stream to the output stream ** @ Param in * input stream * @ Param out * output stream * @ Param buffersize * buffer size * @ throws ioexception * @ mail sunyujia@yahoo.cn * @ since: jun 15,200 8 5:57:24 */public static void in2outstream (inputstream in, outputstream out, int buffersize) throws ioexcep Tion {byte [] buffer = new byte [buffersize]; // buffer for (INT bytesread = 0; (bytesread = in. Read (buffer ))! =-1;) {out. write (buffer, 0, bytesread); arrays. fill (buffer, (byte) 0) ;}}/***** description: write the content in read to writer ** @ Param in * @ Param out * @ Param buffersize * @ throws ioexception * @ mail sunyujia@yahoo.cn * @ since: Jul 2, 2008 11:22:42 pm */public static void read2writer (Reader in, writer out, int buffersize) throws ioexception {char [] buffer = new char [buffersize]; // buffer for (INT bytesread = 0; (byte Sread = in. Read (buffer ))! =-1;) {out. write (buffer, 0, bytesread); buffer = new char [buffersize] ;}}/*** Description: Write the content in read to writer (row by row) ** @ Param in * @ Param out * @ Param buffersize * @ throws ioexception * @ mail sunyujia@yahoo.cn * @ since: Jul 2, 2008 11:22:42 pm */public static void read2writer (bufferedreader in, printwriter out) throws ioexception {for (string line; (line = in. readline ())! = NULL;) {out. println (line) ;}}/***** description: read File return byte array stream ** @ Param file * @ return byte array stream * @ mail sunyujia@yahoo.cn * @ since: jun 15,200 8 4:52:41 */public static bytearrayoutputstream readfiletobytestream (File file) throws ioexception {fileinputstream FD = NULL; bytearrayoutputstream Bos = NULL; try {FS = new fileinputstream (File ); bos = new bytearrayoutputstream (); in2outstream (FCM, Bos, Buffer_size);} finally {If (FS! = NULL) fiis. close ();} return Bos;}/***** description: read File return byte array ** @ Param file * @ return byte array * @ throws ioexception * @ mail sunyujia@yahoo.cn * @ since: jun 15,200 8 5:38:50 */public static byte [] readfiletobytearray (File file) throws ioexception {bytearrayoutputstream Bos = NULL; try {Bos = readfiletobytestream (File );} finally {If (Bos! = NULL) Bos. close ();} return Bos. tobytearray ();}/*** Description: Read the string from the reader object ** @ Param reader * @ return * @ throws ioexception * @ mail sunyujia@yahoo.cn * @ since: jul 2, 2008 11:08:32 */public static string readstring (Reader reader) throws ioexception {printwriter Sw = NULL; bufferedreader in = NULL; try {In = new bufferedreader (Reader ); stringwriter SWR = new stringwriter (); Sw = new printwrite R (SWR, true); read2writer (in, SW); Return SWR. tostring () ;}finally {try {If (in! = NULL) in. Close () ;}finally {If (SW! = NULL) SW. close () ;}}/ ***** description: read File Content ** @ Param file * @ return string content * @ throws ioexception * @ mail sunyujia@yahoo.cn * @ since: jun 15,200 8 5:46:32 */public static string readfiletostring (File file) throws ioexception {return readstring (New filereader (File);}/*** description: copy the file ** @ Param SRC * Source File * @ Param DEST * target file * @ Param cover * Whether to overwrite * @ throws ioexception * @ mail s Unyujia@yahoo.cn * @ since: Jun 15,200 8 6:08:28 */public static void copyfile (File SRC, file DEST, Boolean cover) throws ioexception {fileinputstream in = NULL; fileoutputstream out = NULL; try {If (! DeST. exists () {DeST. createnewfile ();} else if (DEST. exists () & cover) {DeST. delete (); DeST. createnewfile () ;}else {return ;}in = new fileinputstream (SRC); out = new fileoutputstream (DEST); in2outstream (In, out, buffer_size );} finally {try {If (in! = NULL) in. Close () ;}finally {If (OUT! = NULL) out. close () ;}}/ ***** description: write File ** @ Param file * @ Param Str * content * @ throws ioexception * @ mail sunyujia@yahoo.cn * @ since: jun 15,200 8 6:17:24 */public static void writefile (File file, string Str) throws ioexception {printwriter out = NULL; bufferedreader in = NULL; try {If (! File. exists () file. createnewfile (); In = new bufferedreader (New stringreader (STR); out = new printwriter (New bufferedwriter (New filewriter (File); read2writer (In, out );} finally {try {If (in! = NULL) in. Close () ;}finally {If (OUT! = NULL) out. close () ;}}/ ***** Description: Read a string from the console ** @ return read string * @ throws ioexception * @ mail sunyujia@yahoo.cn * @ since: jun 15,200 8 6:42:29 */public static string readstringfromsystemin () throws ioexception {bufferedreader BR = new bufferedreader (New inputstreamreader (system. in); try {return BR. readline ();} finally {If (BR! = NULL) Br. close () ;}/ ***** Description: When the objectinputstream object calls * readobject, object deserialized from bytearrayinputstream *** @ Param Bi * @ return * @ throws ioexception * @ mail sunyujia@yahoo.cn * @ since: jun 15,200 8 7:07:53 */public static objectinputstream buildobjectinputstream (bytearrayinputstream Bi) throws ioexception {return New objectinputstream (BI);}/*** description: when the objectoutputstream object calls * writeobject (o, O object will be serialized to bytearrayoutputstream stream to ** @ Param Bos * byte array stream * @ return object output stream * @ throws ioexception * @ mail sunyujia@yahoo.cn * @ since: jun 15,200 8 7:06:00 */public static objectoutputstream buildobjectoutputstream (bytearrayoutputstream BOS) throws ioexception {return New objectoutputstream (BOS);} public static bufferedreader buildbufferedreader (string Str) {return New bufferedreader (New stringreader (STR);} public static bytearrayinputstream buildbytearrayinputstream (string Str) {return New bytearrayinputstream (Str. getbytes ();} public static bytearrayinputstream buildbytearrayinputstream (byte [] BT) {return New bytearrayinputstream (BT);} public static bufferedreader buildreader (inputstream is) {return New bufferedreader (New inputstreamreader (is);}/***** Description: Test code ** @ Param ARGs * @ throws exception * @ mail sunyujia@yahoo.cn * @ since: jul 3, 2008 8:18:33 */public static void main (string [] ARGs) throws exception {byte [] bootfilebytes = ioutil. readfiletobytearray (new file ("C: // boot. ini "); system. out. println (new string (bootfilebytes); string bootfilestr = readfiletostring (new file ("C: // boot. ini "); system. out. println (bootfilestr); system. out. println (new string (bootfilebytes ). equals (bootfilestr); ioutil. copyfile (new file ("C: // boot. ini "), new file (" C: // boot1.ini "), true); ioutil. writefile (new file ("C: // boot2.ini"), bootfilestr); bytearrayoutputstream Bos = new bytearrayoutputstream (); objectoutputstream OOS = ioutil. buildobjectoutputstream (BOS); OOS. writeobject (new string ("ABCD"); objectinputstream OIS = ioutil. buildobjectinputstream (ioutil. buildbytearrayinputstream (Bos. tobytearray (); system. out. println (OIS. readobject (); system. out. println (ioutil. readstring (New stringreader ("test"); system. out. println (ioutil. readstringfromsystemin ());}}