Results 1 to 6 of 6

Thread: directories

  1. #1

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332

    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

  2. #2
    Lively Member
    Join Date
    Feb 2001
    Location
    Jutland, Denmark
    Posts
    71
    FileDialog fd = new FileDialog(this, "Choos a file");
    fd.setVisible(true);
    String file = (fd.getDirectory()+ fd.getFile());
    jTextField1.setText(file);

  3. #3

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332
    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

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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);} 
     }
    }

  5. #5

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332
    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

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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
  •  



Click Here to Expand Forum to Full Width