java - 将 zip 文件移动到其他目录

标签 java

我有一个文件夹,其中有一个 .dat 文件和一个 .zip 文件,我必须将 .zip 文件移动到另一个目录

我有两个文件夹,其中一个是

1) c:\source folder --> having two files abc.dat and other is abc.zip
2) c:\destination  ---> in which zip shpould be get copied 

请告知如何实现这一目标,我现在所做的事情是......

File directory = new File(sourceFolder);
    File[] listFiles = (mcrpFilePath).listFiles();

 for (File f : listFiles) { 
        if (f.isFile()) {  // ?? here logic to pick up the zip file

        //logic to move the zip file to other directory

            }
        }

最佳答案

使用File.renameTo

Renames the file denoted by this abstract pathname.
Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

这里是Example

或者您可以使用Files#move (如果您使用的是 java 7)

Move or rename a file to a target file.

这里是Example using move()

关于java - 将 zip 文件移动到其他目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18566655/

相关文章:

具有不可序列化部分的 Java 序列化

java - 如何防止使用 Hibernate + JPA 执行不需要的更新语句

java - Java中Upcasted对象如何存储在Heap中

java - 为什么我们将对象存储在接口(interface)引用中?

java - 如何将 solr 日期转换为 javascript 日期?

java - Android 2.3.4 SSL问题

Java Card游戏程序中途停止

java - 排除 Jhipster 并将 Spring Boot 从 1.4.0 升级到 2.0.3

java - 如何解析包含时间单位(ms/ns/ps/fs 等)的字符串以在 Java 中加倍

java - 询问嵌套 for 循环内的 while 循环