使用Java复制文件 [英] copy file using Java

查看:147
本文介绍了使用Java复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码使用Java将文件从一个位置复制到另一位置:

i''m copying a file from one location to another using java using the following code:

InputStream fis = new FileInputStream(sourceFile);
OutputStream fos = new FileOutputStream(destFile);
byte[] databuf = new byte[1024];
int length;
while ((length = fis.read(databuf)) > 0) {

fos.write(databuf, 0, length);

}
fis.close();
fos.close();



但是此程序不能像Windows资源管理器一样快地复制文件.
谁能解释原因?
我的代码或方法有问题吗?
还建议我是否还有其他更好的(有效)方法.



But this program does not copy the file as fast as the windows explorer.
can anyone explain the reason?
is something wrong with my code or my approach?
Also suggest me if there is any other better(efficient) way of doing the same.

推荐答案

来自库通用I/O [
Copying a File or Directory - The Java Tutorials[^]

That should kind of explain the action.

so I would use Files.copy(InputStream in, Path target, CopyOption... options) with the new Java7

or FileUtils.copyFile(new File("in.txt"), new File("out.txt")); from the lib commons I/O[^], which is anyway a pretty nice lib for file operations.


这篇关于使用Java复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆