private final static int BUFFER_SIZE = 16 * 1024;
/**
* 实现文件复制
*
* @param src
* 源文件
* @param dist
* 目标文件
* @throws Exception
*/
protected void copyFile(File src, File dist) throws Exception {
InputStream is = null;
BufferedOutputStream bos = null;
try {
is = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
bos = new BufferedOutputStream(new FileOutputStream(dist),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int temp = 0;
try {
while ((temp = is.read(buffer)) > 0) {
bos.write(buffer, 0, temp);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != bos) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Java 文件复制
最新推荐文章于 2024-07-24 19:35:58 发布