I am writing a program which reads in files and then processes them. At the moment, I am using a BufferedReader to read in a list of files from a txt file. This txt file was generated in a command prompt from doing:
dir > filelist.txt
and then cropping down this to just the filenames.
I can incorporate this into my script by:
//pseudo code(needs further directory specification)
Runtime run = Runtime.getRuntime();//pseudo code(needs further directory specification)
Process process = run.exec("dir > filelist.txt");
This command gives the directory with files in the form:
dd/mm/yyyy hh:mm (filesize) file
Once I have this file I can run a Regex to extract the filename.
Pattern file = Pattern.compile("(\d){2}/(\d){2}/(\d){4}\s(\d){2}(:)(\d){2}\s(\d+)\s(.+)\.txt");
for (String fileline : filelist) {
Matcher matchfile = file.matcher(fileline);
if(matchfile.find()){
filearray.add(matchfile.group().split("\\s")[3]);
}
}
Is there an easier way to get just the file names from a directory within Java SE? This seems a bit long winded.
Use
list()methodYou can further separate out the file and directories, using methods like
///////////////////Edited////////////////
Assume i want to find out whether this File object is a file or a directory…