Java Desktop Application - how to obtain list of files with similar file names in a specific folder
I have a Java Desktop Application, in which at an intermediate stage, some files with the following file names are generated
file-01-1.xml
file-01-2.xml
file-01-3.xml
and so on.
The number of f开发者_如何学编程iles with such names is variable, I want to determine the total number of files of above type of name, so that I can then do further processing on these files. (THese files are generated by a DOS command which does not give number of files generated in its output/number of files generated varies depending on input file, hence this problem).
You can implement pure java solution using File.list()
, File.listFiles()
combining them with FileFilter
. Both methods return arrays, so you can retrieve the array length to get number of files.
This method might be ineffective if number of files is very big (e.g. thousands). In this case I'd suggest you to implement platform specific solution by executing external command like sh ls file* | wc -l
.
You can use a custom FilenameFilter when listing file in the output folder. See: http://download.oracle.com/javase/6/docs/api/java/io/File.html#list%28java.io.FilenameFilter%29
Use File.listFiles(FilenameFilter) or similar methods.
精彩评论