【发布时间】:2019-08-15 19:48:35
【问题描述】:
我需要将所有以 .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");
}
预期输出:
C:\Users\harsshah\workspace\FFPreBatchValidation
.classpath
.project
.settings
bin
kjnk.txt
src
kjnk.txt 应该移动到存档文件夹
【问题讨论】:
-
你文件夹里的文件叫什么名字?
-
您看过 Path.endsWith(String) 上的 Javadoc 了吗?我会引用:
On UNIX for example, the path"foo/bar" ends with "foo/bar" and "bar". It does not end with "r" or "/bar"- 因此whatever.txt不会“以”.txt结尾。
标签: java