Click to See Complete Forum and Search --> : directories
CaptainPinko
Apr 1st, 2002, 01:16 AM
how do i/what class(es) would i use to make/read directories and find files in them?
something like the "FileSystemObject" in VB?
BS
Apr 1st, 2002, 06:05 AM
FileDialog fd = new FileDialog(this, "Choos a file");
fd.setVisible(true);
String file = (fd.getDirectory()+ fd.getFile());
jTextField1.setText(file);
CaptainPinko
Apr 1st, 2002, 02:45 PM
i want to do this w/o a dialog, i'm trying to make a program that will generate a playlist for my MP3 (and it is kinda weird...) so i need to scroll through fil;es and then sort them as such SO I can't use a dialog for more than a base directory, the rest has gotta be from there
Dillinger4
Apr 2nd, 2002, 12:42 AM
java.io.File is where it's at. I used something similar to this to eat through my friends directory structure. Im working on somthing similar written in assembly for the speed.
import java.io.*;
public class DirectoryLister{
public static void main(String[] args){
File entry;
if(args.length == 0){
System.out.println("Please specify a directory for scanning");
return;
}else {
entry = new File(args[0]);
}
if(!entry.exists()){
System.out.println("Unable to process request " + entry.getPath() + " is nonexistent ");
return;
}
listDirectory(entry);
}
public static void listDirectory(File entry){
try{
if(!entry.exists()){
System.out.println(entry.getName() + "not Found");
}
if(entry.isFile()){
System.out.println(entry.getCanonicalPath());
}else if (entry.isDirectory()){
System.out.println(entry.getPath());
String[] filename = entry.list();
if(filename == null) return;
for(int i = 0; i < filename.length; i++){
File item = new File(entry.getPath(), filename[i]);
// list it by a recursive call
listDirectory(item);
}
}
} catch(IOException e){System.out.print("Error:" + e);}
}
}
CaptainPinko
Apr 2nd, 2002, 04:24 PM
:confused: huhmmph...
looks good Dil, but could you maybe :( <drum roll> comment it? <gasp>:eek:
Dillinger4
Apr 3rd, 2002, 03:21 AM
Here is the same code which lists empty directories in addition.
import java.io.*;
public class DirectoryLister{
public static void main(String[] args){
File entry;
if(args.length == 0){
System.out.println("Please specify a directory for scanning");
return;
}else {
entry = new File(args[0]);
}
if(!entry.exists()){
System.out.println("Unable to process request " + entry.getPath() + " is nonexistent ");
return;
}
listDirectory(entry);
}
public static void listDirectory(File entry){
try{
if(!entry.exists()){
System.out.println(entry.getName() + "not Found");
}
if(entry.isFile()){
System.out.println(entry.getCanonicalPath());
}else if (entry.isDirectory()){
String[] filename = entry.list();
if(filename.length == 0){System.out.println(entry.getPath() + " is empty");
return;
}
System.out.println(entry.getPath());
for(int i = 0; i < filename.length; i++){
File item = new File(entry.getPath(), filename[i]);
// list it by a recursive call
listDirectory(item);
}
}
} catch(IOException e){System.out.print("Error:" + e);}
}
}
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.