Package IO; import Java. io. filewriter;/* IO (inout output) stream * IO stream is used to process data transmission between devices * Java operates on data through stream * Java is used to operate on stream objects in Io packages * streams are divided by operation data two types: byte stream and byte stream * streams are classified into input streams by flow, the output stream ** Io stream is often used in the base class * abstract base class * inputstream outputstream * abstract base class * reader writer * of the byte stream. The names of the Child classes derived from these four classes it will use its * parent class name as the suffix of the subclass name **/import Java. io. ioexception;/* since I/O streams are used to manipulate data *, the most common form of data is: file **. First, the Operation file is used for demonstration. ** requirement: on the hard disk, create a file and write some data ** find a file The writer subclass object * filewriter suffix is the parent class name, And the prefix name is the function of the stream object ***/public class demo {public static void main (string [] ARGs) throws ioexception {// todo auto-generated method stub // create a filewriter object. When this object is initialized, it must have an operated file. // And the file will be created under the specified directory. If the directory already contains a file with the same name, the target filewriter = new filewriter ("D: // demo.txt") will be overwritten. // call the write method to write the string to filewriter in the stream. write ("abdfsrewr"); // refresh the buffered data in the stream object and refresh the data to the destination. // filewriter. flush (); // stream resources are closed, but the data in the internal buffer is refreshed before being disabled. // The difference between flush and flush is that after flush is refreshed, you can continue using the stream. Close and refresh the stream to close filewriter. close (); // call flush once first and finally close} package IO; import Java. io. filewriter; import Java. io. ioexception ;/** Write the data of an existing file ** \ r \ newline **/public class demo1 {public static void main (string [] ARGs) throws ioexception {// todo auto-generated method stub // pass a true parameter: indicates that the existing file is not overwritten, and the data is continued at the end of the existing file filewriter = new filewriter ("D: // demo.txt", true); filewriter. write ("ffasfas \ nhh"); filewriter. close () ;}} package IO; import Java. io. filenotfoundexception; import Java. io. filereader; import Java. io. ioexception; Public class demo2 {public static void main (string [] ARGs) throws ioexception {// todo auto-generated method stub // create a file to read the stream object and the file with the specified name must be associated to ensure that the file already exists, if no exception exists, filereader = new filereader ("D: // demo.txt"); // call the read method of the stream reading // int CH = filereader. read (); // system. out. println (char) CH); // filereader. close (); // while (true) {// int CH = filereader. read (); // If (CH =-1) {// break; //} // system. out. P Rintln (char) CH); //} int CH = 0; while (CH = filereader. Read ())! =-1) {system. out. println (char) CH) ;}} package IO; import Java. io. filenotfoundexception; import Java. io. filereader; import Java. io. ioexception; public class demo3 {public static void main (string [] ARGs) throws ioexception {// todo auto-generated method stubfilereader filereader = new filereader ("D: // demo.txt "); // defines an array of characters used to store the characters read. // The read (char []) returns the number of characters read. Char [] Buf = new char [1024]; // int num = filereader. read (BUF); // system. out. println (Num + new string (BUF); // filereader. close (); int num = 0; while (num = filereader. read (BUF ))! =-1) {system. out. println (new string (BUF, 0, num);} filereader. close () ;}} package IO; import Java. io. filenotfoundexception; import Java. io. filereader; import Java. io. ioexception; public class demo4 {public static void main (string [] ARGs) throws ioexception {// todo auto-generated method stubfilereader filereader = new filereader ("D: // demo.txt "); char [] Buf = new char [1024]; int num = 0; while (num = filereader. Read (BUF ))! =-1) {system. out. print (new string (BUF, 0, num);} filereader. close () ;}} package IO; import Java. io. filenotfoundexception; import Java. io. filereader; import Java. io. filewriter; import Java. io. ioexception;/* copy a file on drive C to drive D * principle: * store the file data on drive C to the file on drive D * Step: * 1. create a file in drive d to store data in drive C * 2. define associations between read streams and drive C files * 3. complete data storage through continuous read/write * 4. close resource ***/public class demo5 {public static void main (string [] ARGs) throws ioexceptio N {// todo auto-generated method stub // read a character from drive C and write a character to drive D. // Copy1 (); copy2 ();} public static void copy1 () throws ioexception {// create the destination filewriter = new filewriter ("D: // demo1.txt "); // filereader = new filereader ("D: // noname2.java"); char [] Buf = new char [1024]; int num = 0; while (num = filereader. read (BUF ))! =-1) {system. Out. Print (new string (BUF, 0, num);} int CH = 0; while (CH = filereader. Read ())! =-1) {filewriter. write (CH);} filereader. close (); filewriter. close ();} public static void copy2 () throws ioexception {filewriter = NULL; filereader = NULL; filewriter = new filewriter ("D: // demo2.txt "); filereader = new filereader ("D: // demo.txt"); char [] Buf = new char [1024]; int Len = 0; while (LEN = filereader. read ())! =-1) {filewriter. write (BUF, 0, Len);} filereader. close (); filewriter. close () ;}} package IO; import Java. io. bufferedwriter; import Java. io. filewriter; import Java. io. ioexception;/the appearance of the *** Response Buffer ** increases the efficiency of reading and writing data * corresponding class: * bufferedwriter * bufferedreader * The buffer must be combined with the stream to use * the streaming function on the basis of the stream for enhanced operations * the buffer is created to improve the operation efficiency * so the buffer is created again before, must have a stream object * This buffer provides a cross-platform line break. next line ***/public class demo6 {public static void main (string [] ARGs) throws ioexception {// todo auto-generated method stub // create a character to write the stream object filewriter = new filewriter ("D: // demo.txt "); // to improve the efficiency of character writing to stream 1, the buffer technology bufferedwriter = new bufferedwriter (filewriter) is added; // The Stream object to Improve the efficiency, you can pass in the buffered constructor as a parameter // bufferedwriter. write ("afdfdsf"); // bufferedwriter. newline (); // bufferedwriter. write ("yyyyy"); // bufferedwriter. flush (); For (INT I = 0; I <10; I ++) {bufferedwriter. write ("sfsdfsdf" + I); bufferedwriter. newline (); bufferedwriter. flush ();} // closes the buffer, which is the bufferedwriter Stream object in the buffer. close (); // filewriter. close (); after the buffer is closed, you do not need to close this // as long as the buffer is used, remember to refresh }}
See http://edu.csdn.net/heima for details
Dark Horse programmer-io stream (1)