Processing File Directories

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.

Answer:1

Use list() method

File f = new File("d:\\MyFolder");
String[] fileNames = f.list();

You can further separate out the file and directories, using methods like

isFile()
isDirectory()

///////////////////Edited////////////////

Assume i want to find out whether this File object is a file or a directory...

File f = new File("d:\\MyFolder");

if (f.isDirectory()) {

    System.out.println("Its a directory");
}
Answer:2

With Java 7 you have the nio.2 tools , more complete and useful with Paths and Files classes

Answer:3

Question about ScheduledExecutorService.shceduleAtFixedRate - I schedule taskA to run every 500 millis, which blocks for 1000 millis. Now subsequent executions aren't gonna wait the extra 500 millis, ...

Take any OOPs language with referencing as basic structure like java and c#. For fast execution, they also support primitive types like int and char. I think this is done by storing them on the stack....

Someone asked me if he can run private base function. I told him that of course it is not possible (apart from trick of relection). But what the hell is this: public class MyClass { public ...

We are Using SUN JSF 1.2, WebSphere 7.0 for our application, we are getting ViewExpiredException only during the load testing I have gone through the below link javax.faces.application....