我有一个路径abc,在该路径中有目录/子目录和文件。现在,例如,如果有一个目录' A' &安培; ' B'其中有一些文件,并且有子目录,其中有文件。同样在' B'目录。现在输出应来自' A'目录我们应该得到最后修改过的文件,并在' B' 。目录
我做了这么多。
public class GetLastModifiedFile {
static TreeMap<String, String> map = null;
static Entry<String, String> lastfile;
public static void main(String[] args) throws IOException {
File currentDir = new File("D:\\myfiles"); // current directory
map = new TreeMap<String, String>();
displayDirectoryContents(currentDir);
printFileList();
}
public static void printFileList() {
for (Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " " + value);
}
lastfile = map.lastEntry();
System.out.println("last file is" + " " + lastfile);
}
public static void displayDirectoryContents(File dir) throws IOException {
String dateTime = "0000-00-00 00:00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
displayDirectoryContents(file);
} else {
dateTime = sdf.format(new Date(file.lastModified()));
map.put(dateTime, file.toString());
}
}
}
}
输出 - 我只从所有目录和子目录中获取最后修改的文件。我希望输出就像我在&#34; A&#34;目录应该首先检查它是否有子目录或文件,然后在遍历到其他目录之前应该从中获取最后一个修改过的文件。
答案 0 :(得分:1)
在下面找到一个小片段,它可以实现您想要实现的目标。
出于演示目的,它还会生成一些示例目录和文件。
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.Optional;
import static java.util.concurrent.TimeUnit.MINUTES;
public class FindRecentFileRecursively {
public static void main(String[] args) throws IOException {
// create some dummy directories/files
Path root = Paths.get("/tmp/foobar");
for (char c = 'A'; c <= 'C'; c++) {
Path dir = root.resolve("dir_" + c);
System.out.println("create sample directory " + dir.toString());
Files.createDirectories(dir);
long now = System.currentTimeMillis();
for (int i = 1; i <= 3; i++) {
File file = dir.resolve("file" + i).toFile();
file.createNewFile();
file.setLastModified(now + MINUTES.toMillis(i));
printFileInfo(file);
}
}
System.out.println("list the most recent file per directory");
// find all directories below /tmp/foobar
Files.find(root, Integer.MAX_VALUE, (path, attrs) -> attrs.isDirectory())
// for each directory
.forEach((dir) -> {
try {
// find all contained files
Optional<Path> recentFile = Files.find(dir, 1,
(path, attrs) -> attrs.isRegularFile())
// return the file with the recent last
// modification time
.max((p1, p2)
-> Long.compare(
p1.toFile().lastModified(),
p2.toFile().lastModified()));
// if a file was found
if (recentFile.isPresent()) {
// print modification time and file name
printFileInfo(recentFile.get().toFile());
}
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
});
}
private static void printFileInfo(File file) {
System.out.printf(" mtime: %td.%<tm.%<tY %<tH:%<tM:%<tS file: %s%n",
new Date(file.lastModified()),
file.getAbsolutePath()
);
}
}
输出,如
create sample directory /tmp/foobar/dir_A
mtime: 05.01.2016 15:35:15 file: /tmp/foobar/dir_A/file1
mtime: 05.01.2016 15:36:15 file: /tmp/foobar/dir_A/file2
mtime: 05.01.2016 15:37:15 file: /tmp/foobar/dir_A/file3
create sample directory /tmp/foobar/dir_B
mtime: 05.01.2016 15:35:15 file: /tmp/foobar/dir_B/file1
mtime: 05.01.2016 15:36:15 file: /tmp/foobar/dir_B/file2
mtime: 05.01.2016 15:37:15 file: /tmp/foobar/dir_B/file3
create sample directory /tmp/foobar/dir_C
mtime: 05.01.2016 15:35:15 file: /tmp/foobar/dir_C/file1
mtime: 05.01.2016 15:36:15 file: /tmp/foobar/dir_C/file2
mtime: 05.01.2016 15:37:15 file: /tmp/foobar/dir_C/file3
list the most recent file per directory
mtime: 05.01.2016 15:37:15 file: /tmp/foobar/dir_A/file3
mtime: 05.01.2016 15:37:15 file: /tmp/foobar/dir_B/file3
mtime: 05.01.2016 15:37:15 file: /tmp/foobar/dir_C/file3
更新要查找给定root的每个直接子目录中的最新文件,以下代码段将完成工作。
假设目录结构如
/tmp/foobar
/tmp/foobar/dir_A
/tmp/foobar/dir_A/sub_A
/tmp/foobar/dir_B
/tmp/foobar/dir_B/sub_B
/tmp/foobar/dir_C
/tmp/foobar/dir_C/sub_C
,您从root
目录开始/tmp/foobar
代码将返回/tmp/foobar/dir_A
及以下,/tmp/foobar/dir_B
及以下内容中的最新文件...
System.out.println("list the most recent file per direct subdirectory");
// find all direct directories below /tmp/foobar
Files.find(root, 1, (path, attrs) -> attrs.isDirectory())
// filter out the root itself
.filter(p -> !p.equals(root))
.forEach((dir) -> {
try {
// find all contained files in any subdirectory
Optional<Path> recentFile = Files.find(dir, Integer.MAX_VALUE,
(path, attrs) -> attrs.isRegularFile())
// return the file with the recent last
// modification time
.max((p1, p2)
-> Long.compare(p1.toFile().lastModified(),
p2.toFile().lastModified()));
// if a file was found
if (recentFile.isPresent()) {
// print modification time and file name
printFileInfo(recentFile.get().toFile());
}
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
});
<强>输出强>
mtime: 08.01.2016 13:10:57 file: r:\temp\foobar\dir_A\sub_A\file1
mtime: 08.01.2016 13:11:39 file: r:\temp\foobar\dir_B\sub_B\file3
mtime: 08.01.2016 13:11:19 file: r:\temp\foobar\dir_C\file1
编辑以下代码段也适用于Java 6.
File root = new File("/tmp/foobar");
File[] topDirectories = root.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isDirectory();
}
});
System.out.println("topDirectories = " + Arrays.toString(topDirectories));
for (File directory : topDirectories) {
File recentFile = new File("");
recentFile.setLastModified(0L);
recentFile = getRecentFile(directory, recentFile);
System.out.println("recentFile = " + recentFile);
}
// if you have a huge amount of files in deeply nested directories
// this might need some further tuning
static File getRecentFile(File dir, File baseFile) {
File recentFile = baseFile;
for (File file : dir.listFiles()) {
if (file.isDirectory()) {
recentFile = getRecentFile(file, recentFile);
} else {
if (file.lastModified() > recentFile.lastModified()) {
recentFile = file;
}
}
}
return recentFile;
}