Filechannel is the content in javanio. Javanio (newio) is actually an IO multiplexing model. I won’t elaborate on this. If you want to know something about it, you can baidu.
This paper mainly uses buffer and channel in Java NiO to copy files.
The following is the code for copying the file:
package ch03;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
@Slf4j
public class FileCopy {
private FileInputStream inputStream = null;
private FileOutputStream outputStream = null;
private FileChannel inChannel = null;
private FileChannel outChannel = null;
private double startTime;
public void Copy(String src, String dest){
startTime = System.currentTimeMillis();
CopyFile(src,dest);
log.info("the total time is " + (System.currentTimeMillis() - startTime) + "ms" );
}
private void CopyFile(String src,String dest){
try {
inputStream = new FileInputStream(new File(src));
outputStream = new FileOutputStream(new File(dest));
inChannel = inputStream.getChannel();
outChannel = outputStream.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
int readLength = -1;
while (-1 != (readLength = inChannel.read(buffer))){
buffer.flip();//Switching from read mode to write mode
int outLength = 0;
while (0 != (outLength = outChannel.write(buffer))){
log.info("写入了 --->"+ outLength +"KB" );
}
buffer.clear();
}
outChannel.force(true);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
inputStream.close();
outputStream.close();
inChannel.close();
outChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Read More:
- JAVA: How to Use Multipartfile to upload Files
- JAVA: How to Use Minio to upload pictures
- How to Open Files by Dragging and Dropping in C#
- SpringCloud Use openFeign Multipartfile to Upload Files Error: Current request is not a multipart request
- How to Solve RAR Files Extract Error with sevenzipjbinding (Multithread Processing)
- How to Solve IntelliJ IDEA Error: Cannot determine path to ‘tools.jar‘ library for 17 (C:\Program Files\Java\jd…
- Java: How to use itext to export PDF text absolute positioning (implementation method)
- How to Solve Springboot Upload Files Error: The field XXX exceeds its maximum permitted size of 1048576 bytes
- How to Solve Springboot use thymeleaf template error
- How to Download File via Response (Example Code)
- JAVA: How to use the third-party font library method (using the third-party TTF / TTC font library method)
- When the database table field is set to self incrementing, use the entity class to insert or update the data to solve the error (Hibernate Framework)
- JAVA 8: How to Convert List to Map
- How to Solve Error: No suitable driver found for
- How to Solve Error: Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
- Redis Stand-alone Builds a Master-slave Copy Error [How to Solve]
- Feign calls cannot use “_” to report errors [How to Solve]
- How to Solve IBM MQ Connect Error Code 2035
- How to Solve Linux connecte to the old version of SQL Server Error
- JAVA: How to Convert PDF from Color to Grayscale