byte stream:
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import org.junit.Test; /** * May 3, 2018 at 10:02:16 am * @author <a href="mailto:[email protected]">宋进宇</a> * Demo byte stream */ public class IOStreamDemo { // Know the in and out of the system @Test public void t1() throws IOException { //Get the input stream of the system: InputStream InputStream in = System.in; //Get the output stream of the system: PrintStream PrintStream out = System.out; //Exit the loop only by typing ctrl+z while(in.available() != -1){ //Because the system input and output are byte streams, define a byte data to receive input byte[] buf = new byte[16]; //Record the number of bytes read each time int len = in.read( buf ); // output content from 0 to len out.write( buf, 0, len ); } //Close the flow should be closed in the finally after the catch block, //This is omitted here so that the process does not look messy. . . in.close(); out.close(); } //input content from console and output to file @Test public void t2() throws IOException { //get the console input stream InputStream in = System.in; //create a file File outFile = new File( "testIO_Files/fileIn.txt" ); // determine whether the file exists or not if ( !outFile.exists() ) { //Get the desired parent folder File dir = outFile.getParentFile(); // prevent null pointers if ( dir != null ) { //If the file does not exist, first create all the required parent folders dir.mkdirs(); } //create another file outFile.createNewFile(); } //Create a file output stream FileOutputStream fOut = new FileOutputStream( outFile ); //define a buffer array byte[] buf = new byte[32]; int len = -1; while( ( len = in.read( buf ) ) != -1 ){ // output the content of each input to a file fOut.write( buf, 0, len ); } //Close the flow should be closed in the finally after the catch block, //This is omitted here so that the process does not look messy. . . in.close(); fOut.close(); } // output the contents of the file to the console for display @Test public void t3() throws IOException { //get print stream PrintStream out = System.out; // use the file tested above File inFile = new File( "testIO_Files/fileIn.txt" ); // determine whether the file exists or not if ( !inFile.exists() ) { //Get the desired parent folder File dir = inFile.getParentFile(); // prevent null pointers if ( dir != null ) { //If the file does not exist, first create all the required parent folders dir.mkdirs(); } //create another file inFile.createNewFile(); } //Create a file input stream FileInputStream fIn = new FileInputStream( inFile ); byte[] buf = new byte[8]; int len = -1; while ( ( len = fIn.read( buf ) ) != -1 ) { // print to console out.write(buf, 0, len); } //Close the flow should be closed in the finally after the catch block, //This is omitted here so that the process does not look messy. . . fIn.close(); out.close(); } //Test file copy @Test public void t4() throws IOException { // use the file tested above File inFile = new File( "testIO_Files/fileIn.txt" ); File outFile = new File( "testIO_Files/fileOut.txt" ); // determine whether the file exists or not if ( !inFile.exists() ) { //Get the desired parent folder File dir = inFile.getParentFile(); File dir2 = outFile.getParentFile(); // prevent null pointers if ( dir != null ) { //If the file does not exist, first create all the required parent folders dir.mkdirs(); } if ( dir2 != null ) { dir2.mkdirs (); } //create another file inFile.createNewFile(); outFile.createNewFile(); } //Create a file output stream FileInputStream fis = new FileInputStream(inFile); //Create a file output stream FileOutputStream fos = new FileOutputStream(outFile); byte[] buf = new byte[16]; int len = -1; while ( ( len = fis.read( buf ) ) != -1 ) { fos.write( buf, 0, len ); } // 关 关 fis fis.close(); // close fos again fos.close(); //Simply put, it starts from the source of data output, and gradually closes to the target } }
character stream:
import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import org.junit.Test; /** * May 3, 2018 at 10:58:31 am * @author <a href="mailto:[email protected]">宋进宇</a> * Demonstration character stream */ public class ReadWriteDemo { // Input and output to a file through the console /* By observing the content of the file, it can be found that the Chinese characters are garbled * Reason: MyEclipse I set the UTF-8 encoding, a Chinese character is 3 bytes * And the buf I defined is 8 bytes, at this time only 2 bytes are taken in the third Chinese character * is converted into a string, so garbled characters appear. * There are two solutions: one is to define buf large enough, but it is unrealistic. inadvisable * The other is to use the conversion flow to see the conversion flow demonstration */ @Test public void t1() throws IOException { //get the input stream InputStream in = System.in; //Get the file character output stream FileWriter fw = new FileWriter( "testIO_Files/fileWrite.txt" ); byte[] buf = new byte[8]; int len = -1; while( ( len = in.read( buf ) ) != -1 ){ //This cannot be written directly to the file like a byte stream, it needs to be converted String str = new String( buf, 0, len); fw.write(str); } //close stream in.close(); fw.close(); } // Realize file copy through simple character stream //Only copy files with the same encoding as the running environment, if the encoding is different, garbled characters will appear @Test public void t3() throws IOException { //The copied file directly adopts the file generated in the demo byte stream File inFile = new File( "testIO_Files/fileIn.txt" ); //Create a file character input stream FileReader fr = new FileReader( inFile ); //Create a file character output stream FileWriter fw = new FileWriter( "testIO_Files/fileWrite.txt" ); char[] cbuf = new char[16]; int len = -1; while ( ( len = fr.read( cbuf ) ) != -1 ) { fw.write( cbuf, 0, len ); } //close stream fr.close(); fw.close(); } }
Buffered stream:
1) With buffer is faster than without;
2) It is faster to place the buffer in the middle layer than in the outer layer;
3) Operation by line or block is faster than operation by byte or character (operating with Object stream is faster than byte-character method)
4) The buffer can only be used in combination with the stream, and the function of the stream is enhanced on the basis of the stream.
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; import org.junit.Test; /** * May 3, 2018 at 11:24:03 am * @author <a href="mailto:[email protected]">宋进宇</a> * Demo buffer stream * Buffer stream is actually a decorative design pattern that enhances the functions of various input and output streams */ public class BufferedDemo { //add buffer to byte stream @Test public void t1() throws IOException { //Create a byte buffered input stream BufferedInputStream bis = new BufferedInputStream( System.in ); //Create a byte buffered output stream BufferedOutputStream bos = new BufferedOutputStream( System.out ); while ( bis.available() != -1 ) { byte[] buf = new byte[8]; int len = -1; len = to.read(buf); bos.write( buf, 0, len ); } bis.close(); bos.close(); } //add buffer to character stream @Test public void t2() throws IOException { //get the input stream Scanner sc = new Scanner(System.in); //add buffer to file character stream BufferedWriter bw = new BufferedWriter( new FileWriter( "testIO_Files/fileBuffered.txt" ) ); while( sc.hasNext() ) { String str = sc.nextLine(); bw.write( str ); bw.newLine(); } sc.close(); bw.close(); } //Test whether the buffer really has the ability to enhance the normal stream //First, write 1M bytes of content to the file by the way // Test again whether there is a buffered read speed /* * Test result: adding buffering is faster than no buffering! * Buffering is faster as you get closer to the data source! */ @Test public void t3() throws IOException{ FileOutputStream fos = new FileOutputStream( "testIO_Files/testBuff.txt" ); int i = 0; while(i<1024){ byte[] buf = new byte[1024]; for (int j = 0; j < buf.length; j++) { buf[j] = (byte) j; } fos.write(buf); i++; } fos.close(); ///////////////without buffering//////////////////////// long t1 = System.currentTimeMillis(); DataInputStream df = new DataInputStream( new FileInputStream( "testIO_Files/testBuff.txt" ) ); byte[] buf = new byte[1024]; int len = -1; while( ( len = df.read( buf ) ) != -1 ){ System.out.println( new String( buf, 0, len ) ); } long t2 = System.currentTimeMillis(); df.close(); /////////////////without buffering///////////////////////// ///////////////Add buffer at the outermost layer///////////////////////// long t3 = System.currentTimeMillis(); BufferedInputStream bdf = new BufferedInputStream( new DataInputStream( new FileInputStream( "testIO_Files/testBuff.txt" ) ) ); buf = new byte[1024]; len = -1; while( ( len = bdf.read( buf ) ) != -1 ){ System.out.println( new String( buf ) ); } long t4 = System.currentTimeMillis(); bdf.close(); ///////////////Add buffer at the outermost layer///////////////////////// ///////////////Add buffer in the middle///////////////////////// long t5 = System.currentTimeMillis(); DataInputStream dbf = new DataInputStream( new BufferedInputStream( new FileInputStream( "testIO_Files/testBuff.txt" ) ) ); buf = new byte[1024]; len = -1; while( ( len = dbf.read( buf ) ) != -1 ){ System.out.println( new String( buf ) ); } long t6 = System.currentTimeMillis(); dbf.close(); ///////////////Add buffer at the outermost layer///////////////////////// System.out.println( "No buffering: " + ( t2 - t1 ) ); System.out.println( "Add to outer layer: " + ( t4 - t3 ) ); System.out.println( "Add in the middle: " + ( t6 - t5 ) ); } //Test character buffer stream @Test public void t4() throws IOException{ long t1 = System.currentTimeMillis(); BufferedReader br = new BufferedReader( new BufferedReader( new InputStreamReader( new FileInputStream( "testIO_Files/test.txt" ) ) ) ); String str = null; while ( ( str = br.readLine() ) != null ) { System.out.println( str ); } long t2 = System.currentTimeMillis(); br.close(); ////////////////Four levels of nesting////////////////////////// long t3 = System.currentTimeMillis(); BufferedReader br2 = new BufferedReader( new InputStreamReader( new BufferedInputStream( new FileInputStream( "testIO_Files/test.txt" ) ) ) ); str = null; while ( ( str = br2.readLine() ) != null ) { System.out.println( str ); } long t4 = System.currentTimeMillis(); br2.close(); System.out.println(t2-t1); System.out.println(t4-t3); } }