1 Specifies that two bytes are read at a time
FileInputStream fis = new FileInputStream("c.txt"); int length; byte[] bytes=new byte[2]; while ((length=fis.read(bytes))! =-1){ System.out.println("bytes = " + new String(bytes,0,length)); } fis.close();Copy the code
Log:
bytes = df
bytes = 6y
bytes = y6
bytes = 56
Copy the code
2 Reading and Writing files
System.out.println("start = " + new Date().toLocaleString()); FileInputStream fis = new FileInputStream("c.txt"); FileOutputStream fileOutputStream = new FileOutputStream("c1.txt"); int length; byte[] bytes=new byte[2]; while ((length=fis.read(bytes))! =-1){ System.out.println("bytes = " + new String(bytes,0,length)); System.out.println("toLocaleString = " + new Date().toLocaleString()); fileOutputStream.write(bytes,0,length); } fis.close(); fileOutputStream.close(); System.out.println("end = " + new Date().toLocaleString());Copy the code
700M file start = July 4, 2021 10:55:44 am END = July 4, 2021 10:55:49 amCopy the code
3 Buffer stream reads and writes files (default self-built buffer)
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("c.txt")); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("c2.txt")); int length; while ((length=bufferedInputStream.read())! =-1){ System.out.println("length = " + length); bufferedOutputStream.write(length); } bufferedInputStream.close(); bufferedOutputStream.close();Copy the code
700M file start = July 4, 2021 10:50:29 am end = July 4, 2021 10:50:51 amCopy the code
4 Buffer stream + array reads and writes files
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("c.txt")); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("c3.txt")); int length; byte[] bytes = new byte[1024]; while ((length=bufferedInputStream.read(bytes))! =-1){ System.out.println("length = " + new String(bytes,0,length)); bufferedOutputStream.write(bytes,0,length); } bufferedInputStream.close(); bufferedOutputStream.close();Copy the code
700 m file 1 sCopy the code
Conclusion: When both cache arrays are set to 1024, it takes 4 seconds to read and write basic FileInputStream and 1 second to read and write advanced BufferInputStream. However, when the Byte array is set to 10*1024, both the base stream and the cache stream take 1 second. The efficiency of reading and writing visible files depends largely on the size of the array byte, not the advanced level of the stream.