我需要将所有以.txt结尾的文件移动到存档文件夹中。
我有以下代码,但是if (sourcepath.endsWith(".txt"))不验证文件扩展名。
File directory = new File("Archive");
System.out.println((System.getProperty("user.dir")));
File directory1 = new File (System.getProperty("user.dir"));
File[] files = directory1.listFiles();
if (! directory.exists()) {
directory.mkdir();
for(File f : files ) {
Path sourcePath = Paths.get(f.getName());
System.out.println(sourcePath);
if (sourcePath.endsWith(".txt")){ // Not validating
System.out.println(sourcePath.endsWith(extension));
Files.move(sourcePath, Paths.get("Archive"));
}
}
System.out.println("Clearing Folder.....All Files moved to Archive Directory");
}
Run Code Online (Sandbox Code Playgroud)
预期产量:
C:\Users\harsshah\workspace\FFPreBatchValidation
.classpath
.project
.settings
bin
kjnk.txt
src
Run Code Online (Sandbox Code Playgroud)
kjnk.txt应移至存档文件夹
java ×1