
May 13th, 2008, 04:39 PM
|
|
Registered User
|
|
Join Date: May 2008
Posts: 1
Time spent in forums: 25 m 57 sec
Reputation Power: 0
|
|
Need a beginner help (showing list of files and folders in a directory)
hey ..
im asked to show -using java- the list of directories and files in a specified directory. but if in that directory an other directory exists it lists under it its files and directories.
i could do this by the following code:
Code:
import java.io.File;
public class Explore {
public void getList(String dir_path) {
File f = new File(dir_path);
String[] list = f.list();
File tmp;
for(int i=0; i<list.length; i++)
{
tmp = new File(dir_path+"\\"+list[i]);
if(tmp.isFile())
{
System.out.println("File: "+list[i]);
}
else
{
System.out.println("Dir: "+list[i]);
getList(tmp.getPath());
}
}
}
public static void main(String[] args)
{
String path = "C://Documents and Settings//user//Desktop//Test";
Explore e = new Explore();
e.getList(path);
}
}
for now it's Okayy :
Code:
Dir: Farida
File: haha.txt
File: secondFile.docx
Dir: Rami
Dir: 3abiit
File: dddl.txt
File: hii.txt
File: SA.JAVA
but i need to show them as follow:
Code:
Dir: Farida
File: haha.txt
File: secondFile.docx
Dir: Rami
Dir: 3abiit
File: dddl.txt
File: hii.txt
File: SA.JAVA
thx in advance 
|