如何将文件从一个位置复制到另一个位置?

新手上路,请多包涵

我想将文件从一个位置复制到 Java 中的另一个位置。做这个的最好方式是什么?


这是我到目前为止所拥有的:

 import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
public class TestArrayList {
    public static void main(String[] args) {
        File f = new File(
            "D:\\CBSE_Demo\\Demo_original\\fscommand\\contentplayer\\config");
        List<String>temp=new ArrayList<String>();
        temp.add(0, "N33");
        temp.add(1, "N1417");
        temp.add(2, "N331");
        File[] matchingFiles = null;
        for(final String temp1: temp){
            matchingFiles = f.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.startsWith(temp1);
                }
            });
            System.out.println("size>>--"+matchingFiles.length);

        }
    }
}

这不会复制文件,最好的方法是什么?

原文由 vijayk 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 619
2 个回答

您可以使用 (或任何变体):

 Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING);

另外,我建议使用 File.separator/ 而不是 \\ 以使其兼容多个操作系统, 此处 提供问题/答案。

由于您不确定如何临时存储文件,请查看 ArrayList

 List<File> files = new ArrayList();
files.add(foundFile);

要将 List 文件移动到单个目录中:

 List<File> files = ...;
String path = "C:/destination/";
for(File file : files) {
    Files.copy(file.toPath(),
        (new File(path + file.getName())).toPath(),
        StandardCopyOption.REPLACE_EXISTING);
}

原文由 Menno 发布,翻译遵循 CC BY-SA 3.0 许可协议

更新:

另见 https://stackoverflow.com/a/67179064/1847899

使用流

private static void copyFileUsingStream(File source, File dest) throws IOException {
    InputStream is = null;
    OutputStream os = null;
    try {
        is = new FileInputStream(source);
        os = new FileOutputStream(dest);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }
    } finally {
        is.close();
        os.close();
    }
}

使用频道

private static void copyFileUsingChannel(File source, File dest) throws IOException {
    FileChannel sourceChannel = null;
    FileChannel destChannel = null;
    try {
        sourceChannel = new FileInputStream(source).getChannel();
        destChannel = new FileOutputStream(dest).getChannel();
        destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
       }finally{
           sourceChannel.close();
           destChannel.close();
       }
}

使用 Apache Commons IO 库:

 private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException {
    FileUtils.copyFile(source, dest);
}

使用 Java SE 7 文件类:

 private static void copyFileUsingJava7Files(File source, File dest) throws IOException {
    Files.copy(source.toPath(), dest.toPath());
}

或者试试 Googles Guava :

https://github.com/google/番石榴

文档: https ://guava.dev/releases/snapshot-jre/api/docs/com/google/common/io/Files.html

原文由 Alexander Sidikov Pfeif 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题