Java-将文件复制到新文件或现有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/927203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java- Copy file to either new file or existing file
提问by Jared Oberhaus
I would like to write a function copy(File f1, File f2) f1 is always a file. f2 is either a file or a directory.
我想写一个函数 copy(File f1, File f2) f1 总是一个文件。f2 是文件或目录。
If f2 is a directory I would like to copy f1 to this directory (the file name should stay the same).
如果 f2 是一个目录,我想将 f1 复制到这个目录(文件名应该保持不变)。
If f2 is a file I would like to copy the contents of f1 to the end of the file f2.
如果 f2 是一个文件,我想将 f1 的内容复制到文件 f2 的末尾。
So for example if F2 has the contents:
例如,如果 F2 具有以下内容:
2222222222222
2222222222222
And F1 has the contents
F1有内容
1111111111111
1111111111111
And I do copy(f1,f2) then f2 should become
我做 copy(f1,f2) 然后 f2 应该变成
2222222222222
1111111111111
2222222222222
1111111111111
Thanks!
谢谢!
回答by Elijah
Apache Commons IO to the rescue!
Expanding on Allain's post:
扩展阿兰的帖子:
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2, true); // appending output stream
try {
IOUtils.copy(in, out);
}
finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
Using Commons IO can simplify a lot of the stream grunt work.
使用 Commons IO 可以简化大量的流工作。
回答by Jared Oberhaus
Using the code from the answer by Allain Lolande and extending it, this should address both parts of your question:
使用 Alllain Lolande 的答案中的代码并对其进行扩展,这应该可以解决您的问题的两个部分:
File f1 = new File(srFile);
File f2 = new File(dtFile);
// Determine if a new file should be created in the target directory,
// or if this is an existing file that should be appended to.
boolean append;
if (f2.isDirectory()) {
f2 = new File(f2, f1.getName());
// Do not append to the file. Create it in the directory,
// or overwrite if it exists in that directory.
// Change this logic to suite your requirements.
append = false;
} else {
// The target is (likely) a file. Attempt to append to it.
append = true;
}
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(f1);
out = new FileOutputStream(f2, append);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} finally {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
}
回答by Allain Lalonde
FileOutputStream has a constructor that allows you to specify append as opposed to overwrite.
FileOutputStream 有一个构造函数,允许您指定追加而不是覆盖。
You can use this to copy the contents of f1 to the end of f2 as below:
您可以使用它来将 f1 的内容复制到 f2 的末尾,如下所示:
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2, true);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
回答by Mr. Will
Check out the link below. it is a source file that copies a file to another using NIO.
查看下面的链接。它是一个使用 NIO 将文件复制到另一个文件的源文件。
http://www.java2s.com/Code/Java/File-Input-Output/CopyafileusingNIO.htm
http://www.java2s.com/Code/Java/File-Input-Output/CopyafileusingNIO.htm

