java 如何从java中的文件夹中获取单个文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3360255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a single file from a folder in java?
提问by Saikios
The idea is to take one single file, but I don't want to list all of the files. I have the address of the specified folder, but not the name.
这个想法是取一个文件,但我不想列出所有文件。我有指定文件夹的地址,但没有名称。
Basically I want
基本上我想要
findFileInFolder(String folderName) --- this method returns a random filename or the oldest file created on the folder
findFileInFolder(String folderName) --- 此方法返回一个随机文件名或在文件夹上创建的最旧的文件
Has anybody ever tried doing this? Any ideas to avoid listing all of the files in an array and then taking the first one?
有没有人试过这样做?有什么想法可以避免列出数组中的所有文件然后取第一个?
Added:
添加:
Just in case I wasn't clear (I'm really sorry for my English. Please forgive me if I sound prepotent or aggressive it is really not my intentions.) The file is not selected by a human, it's selected by the machine without asking or showing the file except for the method that returns a string with the FileName
以防万一我不清楚(我真的很抱歉我的英语。请原谅我如果我听起来有优势或咄咄逼人,这真的不是我的本意。)该文件不是由人选择的,它是由机器选择的,没有询问或显示文件,除了返回带有 FileName 的字符串的方法
String findFileInFolder(String folderName)
String findFileInFolder(String folderName)
Like I comment is for usage of ram and processor because this is a secondary process and not the primary of the project, so if I have to read over a thousand files it will considerably reduce the performance of my project :(
就像我评论的是内存和处理器的使用一样,因为这是一个辅助进程而不是项目的主要进程,所以如果我必须阅读一千多个文件,它将大大降低我的项目的性能:(
Thanks ;)
谢谢 ;)
Update: the program is running on different computers so if I could just access the directory not "thinking" about reading which file it would be great. =D
更新:该程序在不同的计算机上运行,所以如果我可以只访问目录而不是“考虑”读取哪个文件会很棒。=D
Hopefully last update: sorry for bothering you guys :)
希望最后更新:抱歉打扰你们:)
From what I read on the answers there is no way. My question is: what good alternatives instead of doing an array would you think? My idea is to create an index in a textfile and to take only the first line.
从我对答案的阅读来看,没有办法。我的问题是:你认为有什么好的替代方案而不是做数组?我的想法是在文本文件中创建一个索引并只取第一行。
采纳答案by Saikios
I decidee to use this code, is not exactly what I wanted but it works for now.
我决定使用此代码,这不是我想要的,但现在可以使用。
public static String getFileToCrawl(String directory){
File dir = new File(directory);
String[] children = dir.list();
if (children == null) {
return "";
} else {
int i=0;
String filename = children[i];
while (i<children.length && !filename.contains(".txt")){
i++;
filename = children[i];
}
return filename;
}
}
if anyone like it or know a way to improve this code it's really welcome ;) if you want to use it feel free :D
如果有人喜欢它或知道改进此代码的方法,那真的很受欢迎;) 如果您想使用它,请随意:D
回答by Manuel Selva
You can have a look on FileFilterclass and on the public File[] listFiles(FileFilter filter)
你可以看看FileFilter班级和public File[] listFiles(FileFilter filter)
Using this method you will be able to filter files according to their last modification date for example.
例如,使用此方法,您将能够根据文件的最后修改日期过滤文件。
On a side note, why do you want to avoid to list all the files, for performances concerns ?
附带说明一下,出于性能考虑,您为什么要避免列出所有文件?
回答by Denis Tulskiy
There is no way of doing this in current versions of java other than listing all files and selecting what you need. If you can use java 7, there is a FileVisitorclass that can walk a folder tree without listing all of the files.
除了列出所有文件并选择您需要的文件之外,在当前版本的 java 中没有办法做到这一点。如果您可以使用 java 7,则有一个FileVisitor类可以在不列出所有文件的情况下遍历文件夹树。
回答by Paul M.
I realize this is an old thread but here is a quick and dirty way to do it:
我意识到这是一个旧线程,但这是一种快速而肮脏的方法:
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
public class Shuffle {
public static void main(String[] argv)
throws Exception {
File dir = new File(".");
String[] children = dir.list();
Collections.shuffle(Arrays.asList(children));
System.out.println(children[0]);
}
}

