在不同位置复制和重命名文件 [英] Copy and rename file on different location
本文介绍了在不同位置复制和重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文件 example.tar.gz ,我需要将其复制到其他名称不同的位置例如 _test.tar.gz .我尝试过
I have one file example.tar.gz and I need to copy it to another location with different name example _test.tar.gz. I have tried with
private void copyFile(File srcFile, File destFile) throws IOException {
InputStream oInStream = new FileInputStream(srcFile);
OutputStream oOutStream = new FileOutputStream(destFile);
// Transfer bytes from in to out
byte[] oBytes = new byte[1024];
int nLength;
BufferedInputStream oBuffInputStream = new BufferedInputStream(oInStream);
while((nLength = oBuffInputStream.read(oBytes)) > 0) {
oOutStream.write(oBytes, 0, nLength);
}
oInStream.close();
oOutStream.close();
}
其中
String from_path = new File("example.tar.gz");
File source = new File(from_path);
File destination = new File("/temp/example_test.tar.gz");
if(!destination.exists())
destination.createNewFile();
然后
copyFile(source, destination);
它不起作用.路径正确.它打印该文件存在.有人可以帮我吗?
It doesn't work. The path is correct. It prints that the file exists. Can anybody help me?
推荐答案
Why to reinvent the wheel, just use FileUtils.copyFile(File srcFile, File destFile)
, this will handle many scenarios for you
这篇关于在不同位置复制和重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文