|
-
Apr 1st, 2002, 01:16 AM
#1
Thread Starter
Hyperactive Member
directories
how do i/what class(es) would i use to make/read directories and find files in them?
something like the "FileSystemObject" in VB?
Last edited by CaptainPinko; Apr 1st, 2002 at 02:25 AM.
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
-
Apr 1st, 2002, 06:05 AM
#2
Lively Member
FileDialog fd = new FileDialog(this, "Choos a file");
fd.setVisible(true);
String file = (fd.getDirectory()+ fd.getFile());
jTextField1.setText(file);
-
Apr 1st, 2002, 02:45 PM
#3
Thread Starter
Hyperactive Member
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
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
-
Apr 2nd, 2002, 12:42 AM
#4
Dazed Member
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.
Code:
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);}
}
}
Last edited by Dilenger4; Apr 3rd, 2002 at 01:54 AM.
-
Apr 2nd, 2002, 04:24 PM
#5
Thread Starter
Hyperactive Member
huhmmph...
looks good Dil, but could you maybe <drum roll> comment it? <gasp>
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
-
Apr 3rd, 2002, 03:21 AM
#6
Dazed Member
Here is the same code which lists empty directories in addition.
Code:
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);}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|