一、引言
文件复制是计算机操作中常见的应用场景之一,也是Java编程中的常见问题。文件复制的实现有多种方式,Java提供了多种API,如io、nio、Channel等,本文将介绍如何使用Java API来实现文件复制。
二、Java IO 文件复制
1. 文件读取
在进行文件复制之前,需要先将源文件读取到内存中。Java IO提供了文件读取相关的API,最常见的就是使用FileInputStream来读取一个文件。
FileInputStream fis = null;
byte[] buffer = new byte[1024];
try {
fis = new FileInputStream(srcFile);
int len;
while ((len = fis.read(buffer)) != -1) {
//将文件信息读取到缓存中
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
2. 文件写入
文件读取到缓存中后,需要将其写入到目标文件中。Java IO提供了文件写入相关的API,最常见的就是使用FileOutputStream来写入一个文件。
FileOutputStream fos = null;
try {
fos = new FileOutputStream(destFile);
fos.write(buffer, 0, len);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
3. 文件复制
将文件读取和写入组合在一起,即可实现文件复制操作。
public static void copyFile(String srcPath, String destPath){
File srcFile = new File(srcPath);
File destFile = new File(destPath);
FileInputStream fis = null;
FileOutputStream fos = null;
byte[] buffer = new byte[1024];
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
int len;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
三、Java NIO 文件复制
Java NIO是Java IO的进阶版,提供更高效的I/O操作和更灵活的文件处理方式。在Java NIO中,文件操作主要基于Channel和Buffer。
1. 文件读取
使用Java NIO进行文件读取,需要使用FileChannel来代替FileInputStream。
FileInputStream fis = null;
FileChannel fcin = null;
ByteBuffer buffer = ByteBuffer.allocate(1024);
try {
fis = new FileInputStream(srcFile);
fcin = fis.getChannel();
while (fcin.read(buffer) != -1) {
buffer.flip();
//将文件信息读取到缓存中
buffer.clear();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fcin.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
2. 文件写入
使用Java NIO进行文件写入,需要使用FileChannel来代替FileOutputStream。
FileOutputStream fos = null;
FileChannel fcout = null;
ByteBuffer buffer = ByteBuffer.allocate(1024);
try {
fos = new FileOutputStream(destFile);
fcout = fos.getChannel();
buffer.put(byteBuffer).flip();
fcout.write(buffer);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fcout.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
3. 文件复制
将文件读取和写入组合在一起,即可实现文件复制操作。
public static void copyFileByNIO(String srcPath, String destPath) {
File srcFile = new File(srcPath);
File destFile = new File(destPath);
FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel fcin = null;
FileChannel fcout = null;
ByteBuffer buffer = ByteBuffer.allocate(1024);
try {
fis = new FileInputStream(srcFile);
fcin = fis.getChannel();
fos = new FileOutputStream(destFile);
fcout = fos.getChannel();
while (fcin.read(buffer) != -1) {
buffer.flip();
fcout.write(buffer);
buffer.clear();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fcin.close();
fis.close();
fcout.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
四、结论
本文主要介绍了在Java中实现文件复制的两种常见方式,一种是使用Java IO进行文件读取和写入,另一种是使用Java NIO进行文件读取和写入。两种方式各自有其优缺点,具体使用时需要根据情况进行选择。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/238853.html
微信扫一扫
支付宝扫一扫