【发布时间】:2014-11-03 10:47:18
【问题描述】:
如何列出文件系统中可用的文件的起始编号和结束编号?
如果C:\Test\中有500文件,那么如何列出从1到20的文件
根据此列表提供start number and end number 特定文件路径可用的文件。
我在java中尝试这个
我尝试了这样的事情,它为我提供了给定路径的所有可用文件
public static List<String> loadAllFiles(String filesLocation) {
//find OS
//String osName = System.getProperty("os.name");
//replace file path based on OS
filesLocation = filesLocation.replaceAll("\\\\|/", "\\"+System.getProperty("file.separator"));
List<String> pdfFiles = new ArrayList<String>();
if (log.isDebugEnabled()) {
log.debug("In loadAllFiles execute start");
}
File directoryList = new File(filesLocation);
File[] filesList = directoryList.listFiles();
try {
for (int count = 0; count < filesList.length; count++) {
if (!filesList[count].isDirectory() && filesList[count].getName().endsWith(SPLIT_AND_SAVE_WORKING_FILE_EXTENSION.trim())) {
// load only PDF files
pdfFiles.add(filesList[count].getName().replace(SPLIT_AND_SAVE_WORKING_FILE_EXTENSION.trim(), ""));
}
}
} catch (Exception filesException) {
filesException.printStackTrace();
//TODO : Log the exception
} finally {
if (filesList != null)
filesList = null;
if (directoryList != null)
directoryList = null;
}
log.debug("In loadAllFiles execute end");
return pdfFiles;
}
I think the question is misunderstood, Say if i have 1000 files[file names can be anything] and i want to restrict getting the files name like i will give starting Number and ending number. like 1 to 20 and i want to load those 20 files alone.
【问题讨论】:
-
再次阅读您的问题后。您想仅列出 500 个列表中的前 20 个文件吗?或者您想列出以 numb2er 1 到 20 开头的文件?以 100 开头的文件呢?
-
是的,我只想列出 500 个列表中的前 20 个文件。感谢理解
-
我添加了两个新示例来列出目录中的前 20 个文件。
标签: java file file-handling