JavaEE learning log continuously updated ----> must-see! JavaEE learning route (article summary)
Analog copy and paste
Byte stream analog copy and paste
Clear :
- Data source: D: \ jdk-11 Chinese api revision .CHM
- Destination: E: \ jdk-11 Chinese api revision .CHM
Copied files : 56.4 MB (59,176,862 bytes)
Step copied files:
- Create FileInputStream file byte input stream of objects, the construction method to read the bound data source
- Create a file output stream of bytes FileOutputStream object constructor to bind the destination to be written
- FileInputStream object using the read method of reading the file
- Use write FileOutputStream object, writes the read data to the destination
- The release of resources (to write off, after reading off)
note:
- Once read multiple bytes, a plurality of bytes written manner can greatly improve the efficiency of
- The computer may be configured and memory usage, to adjust the size of the byte array , within a certain range, the greater the byte array, the higher the efficiency. Outside this range, the memory footprint is too large, it will lead to lower efficiency.
Efficiency Test: 56.4 MB (59,176,862 bytes) Total time to copy files to 659ms
Code Example:
public class Demo01 {
public static void main(String[] args) throws IOException {
long s = System.currentTimeMillis();
//1.创建文件字节输入流的FileInputStream对象,构造方法中绑定要读取的数据源
FileInputStream fis = new FileInputStream("d:\\jdk-11中文api修订版.CHM");
//2.创建文件字节输出流FileOutputStream对象,构造方法中绑定要写入的目的地
FileOutputStream fos = new FileOutputStream("e:\\jdk-11中文api修订版.CHM");
//一次读取一个字节,写一个字节的方式复制文件
//3.使用FileInputStream对象中的方法read读取文件
// int len = 0;
// while((len = fis.read())!=-1){
// //4.使用FileOutputStream对象中的方法write,把读取到的数据写入到目的地中
// fos.write(len);
// }
//一次读取一个字节,效率太慢!
//使用一次读取多个字节,写入多个字节的方式
byte[] bytes = new byte[1024*120];
int len = 0;
while((len = fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
//5.释放资源(先关写,后关读)
fos.close();
fis.close();
long e = System.currentTimeMillis();
System.out.println("复制文件共耗时:"+(e-s)+"ms");
}
}
Analog copy and paste buffer flow (more efficient)
Copy files using buffered streams: high efficiency
Use steps :
- Create BufferedInputStream byte buffer input stream objects, passing the resulting objects FileInputStream
- Create BufferedOutputStream byte buffer input stream objects, passing the resulting FileOutputStream object
- Use BuffedInputStream object's method read, read the file
- A method using an object BufferedOutputStream write, writing the read data buffer
- The release of resources (will first refresh)
Efficiency Test: 56.4 MB (59,176,862 bytes) Total time to copy files to 164ms
Code Example:
public class Demo02 {
public static void main(String[] args) throws IOException {
long s = System.currentTimeMillis();
//1.创建字节缓冲输入流BufferedInputStream对象,构造方法传递FileInputStream对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:\\jdk-11中文api修订版.CHM"));
//2.创建字节缓冲输入流BufferedOutputStream对象,构造方法传递FileOutputStream对象
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("e:\\jdk-11中文api修订版.CHM"));
byte[] bytes = new byte[1024];
int len = 0;
while((len = bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
//5.释放资源(先关写,后关读)
bos.close();
bis.close();
long e = System.currentTimeMillis();
System.out.println("复制文件共耗时:"+(e-s)+"ms");
}
}