get all files in a Directory

Hi,how can get all the file names in a local directory using core java classes.Thanlks in advance
[118 byte] By [veerjaa] at [2007-11-26 18:32:21]
# 1
Hello,Read the API for the java.io.File class @ http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html
virusakosa at 2007-7-9 6:06:28 > top of Java-index,Java Essentials,New To Java...
# 2

Hi there,

Check out with code metioned below....

import java.io.File;

import java.util.List;

import java.util.Arrays;

/**

* @Author Rahul

**/

public class FileList {

private String Dirname;

private File file;

public FileList(String Dirname){

this.Dirname = Dirname;

this.file = null;

}

public FileList(File file){

this.file = file;

this.Dirname = null;

}

/** Returns List of FileNames In the Directory / Folder

*& returns null when the specified FileObject / Dirname is not a directory

**/

public List<String> getFileNamesList(){

List<String> Flist = null;

File f;

if(this.Dirname != null && this.file == null){

f = new File(Dirname);

}

else{

f = this.file;

}

if(f.isDirectory() == true){

String s[] = f.list();

Flist = Arrays.asList(s);

}

return(Flist);

}

/** Returns List of File Object inside Specified Dirname / File Object

*& returns null when the specified FileObject / Dirname is not a directory

**/

public List<File> getFilesList(){

List<File> Flist = null;

File f;

if(this.Dirname != null && this.file == null){

f = new File(this.Dirname);

}

else{

f = this.file;

}

if(f.isDirectory() == true){

File fil[] = f.listFiles();

Flist = Arrays.asList(fil);

}

return(Flist);

}

public static void main(String s[]){

System.out.println(new FileList("C:\\WMSDK").getFileNamesList());

}

}

And, an advice !! i kind of support my fellow posters view.If u r a newbie it would be gr8 if can first check whether there are any related classes which comes in built with JDK or any other 3rd party libraries or frameworks and then read the API documentation provided by the vendor and then try to search for an implementation by googling and finally try it out.

This would be a gr8 practise and helps building your knowledge.

else where it would be a herculian task to catch wid all the things.

Hope it might be of some help :)

REGARDS,

RaHuL

RahulSharnaa at 2007-7-9 6:06:28 > top of Java-index,Java Essentials,New To Java...
# 3

> Hi,

>

> how can get all the file names in a local

> directory using core java classes.

>

> Thanlks in advance

String directoryName = "C:\\dir\sub_dir";

File file = new File(directoryName);

if (file.isDirectory)

String[] files = file.list(); // <===Bingo, there's your list.

~Bill

abillconsla at 2007-7-9 6:06:28 > top of Java-index,Java Essentials,New To Java...