我想一劳永逸地读到多个档案。 姓名如下:
- nnnnn_UM2012.txt
- ghkjdf_UM2045.txt
- erey_UM2189.txt
- ....
有1 000多份档案,我不想用类似于以下的法典,在 Java一处书写所有档案:
String fileNames = {"nnnnn_UM2012.txt","ghkjdf_UM2045.txt","erey_UM2189.txt", …}
或许应当按相反顺序阅读档案名称。 我如何能够这样做?
我想一劳永逸地读到多个档案。 姓名如下:
有1 000多份档案,我不想用类似于以下的法典,在 Java一处书写所有档案:
String fileNames = {"nnnnn_UM2012.txt","ghkjdf_UM2045.txt","erey_UM2189.txt", …}
或许应当按相反顺序阅读档案名称。 我如何能够这样做?
将所有档案放在一个文件夹中(文件夹在档案清单中):
// get all files in the folder
final File folder = new File(".");
final List<File> fileList = Arrays.asList(folder.listFiles());
a. 将所有档案放在一个文件夹中,但不包括分文件夹:
// get all files in the folder excluding sub-folders
final File folder = new File(".");
final List<File> fileList = Arrays.asList(folder.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return pathname.isFile();
}
}));
3. 将档案清单改为对案件敏感的指令:
// sort the files into reverse order
Collections.sort(fileList, new Comparator<File>() {
public int compare(File o1, File o2) {
return o2.getName().compareTo(o1.getName());
}
});
To sort the list of files into reverse case-insensitive order:
// sort the files into reverse order ignoring case
Collections.sort(fileList, new Comparator<File>() {
public int compare(File o1, File o2) {
return o2.getName().compareToIgnoreCase(o1.getName());
}
});
You can use listFiles method to obtain all the files in the folder.
File rep = new File("path to rep");
File[] list = rep.listFiles();
ArrayList<String> filenames = new ArrayList<String>();
for ( int i = 0; i < list.length; i++) {
filenames.add(list[i].getName());
}
我认为这是解决你问题的办法。
You can follow the below approach if all the files are in a single directory.
Get a reference to the directory by providing its fully qualified path and then use the list()
function get all the file names into a String
array inside the directory.
在采取这一步骤之后,你可以按照你想要的方式对档案进行分类(例如,用名称、括号、名称)。
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...