我想在Java目录中移动,但我不知道它的名字? Java是否提供了这样做的任何功能?
File srcFile = "C:/Entertainment/XXXXXXX/break.avi"
我确信娱乐中只有一个目录,但我不知道它的名字。如何在XXXXXXX目录中移动以访问其中的任何文件?
任何帮助?
答案 0 :(得分:4)
尝试,
File file = new File("C:/Entertainment");
File[] files = file.listFiles();
答案 1 :(得分:3)
File srcDir = new File("C:/Entertainment/");
File srcFile = null;
for (File dirMember : srcDir.listFiles()) {
if (!dirMember.isDirectory()) {
continue; // we don't need regular files
}
if (dirMember.getName().equals(".")) {
continue; // we don't need this directory
}
if (dirMember.getName().equals("..")) {
continue; // we don't need the parent directory
}
// This is the one you need.
srcFile = new File(new File(srcDir, dirMember.getName()), "break.avi");
}
您还可以使用FileFilter,例如来自Commons IO
的FileFilterFile dir = new File("C:/Entertainment/");
File[] files = dir.listFiles( DirectoryFileFilter.INSTANCE ); // returns all subdirs
srcFile = new File(files[0], "break.avi");
答案 2 :(得分:1)
File srcFile = new File("C:\\Entertainment");
srcFile = new File(srcFile, srcFile.list()[0]);
srcFile = newFile(srcFile, "break.avi");
System.out.println(srcFile.getPath());
答案 3 :(得分:0)
File outer= new File("C:/test");
File inner = outer.listFiles()[0];//if you are sure there is one
File[] listOfFilesInInnerMost = inner.listFiles();
System.out.println("Files: " + Arrays.asList(listOfFilesInInnerMost));
将打印
文件:[C:\ test \ something \ text(2).txt,C:\ test \ something \ text(3).txt,C:\ test \ something \ text.txt]