use*_*155 147 java directory copy file
我想使用Java将文件从一个目录复制到另一个目录(子目录).我有一个目录,dir,带有文本文件.我在dir中遍历前20个文件,并希望将它们复制到dir目录中的另一个目录,这是我在迭代之前创建的.在代码中,我想复制review(代表第i个文本文件或审查)trainingDir.我怎样才能做到这一点?似乎没有这样的功能(或者我找不到).谢谢.
boolean success = false;
File[] reviews = dir.listFiles();
String trainingDir = dir.getAbsolutePath() + "/trainingData";
File trDir = new File(trainingDir);
success = trDir.mkdir();
for(int i = 1; i <= 20; i++) {
File review = reviews[i];
}
Run Code Online (Sandbox Code Playgroud)
Aru*_*hny 163
现在这应该可以解决你的问题
File source = new File("H:\\work-temp\\file");
File dest = new File("H:\\work-temp\\file2");
try {
FileUtils.copyDirectory(source, dest);
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
FileUtils来自apache commons-io库的类,自1.2版开始提供.
使用第三方工具而不是自己编写所有实用程序似乎是一个更好的主意.它可以节省时间和其他宝贵的资源.
Mic*_*rdt 40
标准API(尚未)中没有文件复制方法.你的选择是:
Gle*_*est 34
在Java 7中,是在Java中复制文件的标准方法:
Files.copy.
它集成了O/S本机I/O以实现高性能.
请参阅我的A 标准简洁方法来复制Java中的文件?有关用法的完整描述.
Bri*_*ian 26
以下来自Java Tips的示例非常简单.我已经切换到Groovy处理文件系统的操作 - 更容易和更优雅.但这里是我过去使用过的Java Tips.它缺乏强大的异常处理功能,这使得它非常简单.
public void copyDirectory(File sourceLocation , File targetLocation)
throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists()) {
targetLocation.mkdir();
}
String[] children = sourceLocation.list();
for (int i=0; i<children.length; i++) {
copyDirectory(new File(sourceLocation, children[i]),
new File(targetLocation, children[i]));
}
} else {
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
Run Code Online (Sandbox Code Playgroud)
Jan*_*usz 19
如果要复制文件而不移动它,可以像这样编码.
private static void copyFile(File sourceFile, File destFile)
throws IOException {
if (!sourceFile.exists()) {
return;
}
if (!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
if (destination != null && source != null) {
destination.transferFrom(source, 0, source.size());
}
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
Run Code Online (Sandbox Code Playgroud)
Bal*_*man 14
apache commons Fileutils很方便.你可以做以下活动.
将文件从一个目录复制到另一个目录.
使用 copyFileToDirectory(File srcFile, File destDir)
将目录从一个目录复制到另一个目录.
使用 copyDirectory(File srcDir, File destDir)
将一个文件的内容复制到另一个文件
使用 static void copyFile(File srcFile, File destFile)
Igo*_*bak 14
Spring Framework有很多类似的util类,比如Apache Commons Lang.所以有org.springframework.util.FileSystemUtils
File src = new File("/home/user/src");
File dest = new File("/home/user/dest");
FileSystemUtils.copyRecursively(src, dest);
Run Code Online (Sandbox Code Playgroud)
File sourceFile = new File("C:\\Users\\Demo\\Downloads\\employee\\"+img);
File destinationFile = new File("\\images\\" + sourceFile.getName());
FileInputStream fileInputStream = new FileInputStream(sourceFile);
FileOutputStream fileOutputStream = new FileOutputStream(
destinationFile);
int bufferSize;
byte[] bufffer = new byte[512];
while ((bufferSize = fileInputStream.read(bufffer)) > 0) {
fileOutputStream.write(bufffer, 0, bufferSize);
}
fileInputStream.close();
fileOutputStream.close();
Run Code Online (Sandbox Code Playgroud)
爪哇 8
Path sourcepath = Paths.get("C:\\data\\temp\\mydir");
Path destinationepath = Paths.get("C:\\data\\temp\\destinationDir");
Files.walk(sourcepath)
.forEach(source -> copy(source, destinationepath.resolve(sourcepath.relativize(source))));
Run Code Online (Sandbox Code Playgroud)
复制方式
static void copy(Path source, Path dest) {
try {
Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
Run Code Online (Sandbox Code Playgroud)
你似乎在寻找简单的解决方案(一件好事).我建议使用Apache Common的FileUtils.copyDirectory:
将整个目录复制到保留文件日期的新位置.
此方法将指定的目录及其所有子目录和文件复制到指定的目标.目标是目录的新位置和名称.
如果目标目录不存在,则创建目标目录.如果目标目录确实存在,则此方法将源与目标合并,并且源优先.
您的代码可能像这样简单明了:
File trgDir = new File("/tmp/myTarget/");
File srcDir = new File("/tmp/mySource/");
FileUtils.copyDirectory(srcDir, trgDir);
Run Code Online (Sandbox Code Playgroud)
Apache commons FileUtils会很方便,如果你只想将文件从源目录移动到目标目录而不是复制整个目录,你可以这样做:
for (File srcFile: srcDir.listFiles()) {
if (srcFile.isDirectory()) {
FileUtils.copyDirectoryToDirectory(srcFile, dstDir);
} else {
FileUtils.copyFileToDirectory(srcFile, dstDir);
}
}
Run Code Online (Sandbox Code Playgroud)
如果要跳过目录,可以执行以下操作:
for (File srcFile: srcDir.listFiles()) {
if (!srcFile.isDirectory()) {
FileUtils.copyFileToDirectory(srcFile, dstDir);
}
}
Run Code Online (Sandbox Code Playgroud)
import static java.nio.file.StandardCopyOption.*;
...
Files.copy(source, target, REPLACE_EXISTING);
Run Code Online (Sandbox Code Playgroud)
资料来源:https: //docs.oracle.com/javase/tutorial/essential/io/copy.html
受到莫希特(Mohit)在该主题中的回答的启发。仅适用于Java 8。
以下内容可用于将所有内容从一个文件夹递归复制到另一个文件夹:
public static void main(String[] args) throws IOException {
Path source = Paths.get("/path/to/source/dir");
Path destination = Paths.get("/path/to/dest/dir");
List<Path> sources = Files.walk(source).collect(toList());
List<Path> destinations = sources.stream()
.map(source::relativize)
.map(destination::resolve)
.collect(toList());
for (int i = 0; i < sources.size(); i++) {
Files.copy(sources.get(i), destinations.get(i));
}
}
Run Code Online (Sandbox Code Playgroud)
流式FTW。
更新于2019-06-10:重要说明-关闭由Files.walk调用获取的流(例如,使用try-with-resource)。感谢@jannis的观点。
pha*_*ace -4
你使用renameTo() \xe2\x80\x93 并不明显,我知道......但它是 Java 中的 move 等价物......
\n| 归档时间: |
|
| 查看次数: |
407835 次 |
| 最近记录: |