Fedora Linux Support Community & Resources Center
  #1  
Old 27th August 2008, 09:48 PM
keatonvictor
Guest
 
Posts: n/a
Talking Java: Listing all files and folders, including sub directories

Hi Everyone

I am trying to write a java program that simple outputs to a text file using >> file.txt pipes.

The program needs to start are one directory and then travel througha ll sub directories listing all files and folders within. I have includes my code so far, but all it does is list 2 directories deep, i need some kind of recursion but im a bit new at this, please help....


import java.io.File;

public class DirectoryReader {

public static void main(String[] args) {

String dir = "/";
File folder = new File(dir);
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("[FILE]" + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {

String directoryToSearch = listOfFiles[i].getName();
System.out.println("[DIR]" + directoryToSearch);

File subFolder = new File(dir + directoryToSearch);
File[] listOfSubFiles = subFolder.listFiles();
for (int j = 0; j < listOfSubFiles.length; j++){
System.out.println("\t[DIR]" + listOfSubFiles[j].getName());
}

}
}
}

}
Reply With Quote
  #2  
Old 8th September 2008, 06:42 AM
Skunk Worx's Avatar
Skunk Worx Offline
Registered User
 
Join Date: Apr 2006
Posts: 427
old post, but just fooling around (i'm no java programmer) :

Code:
import java.io.File;

public class DirectoryReader {

  static int spc_count=-1;

  static void Process(File aFile) {
    spc_count++;
    String spcs = "";
    for (int i = 0; i < spc_count; i++)
      spcs += " ";
    if(aFile.isFile())
      System.out.println(spcs + "[FILE] " + aFile.getName());
    else if (aFile.isDirectory()) {
      System.out.println(spcs + "[DIR] " + aFile.getName());
      File[] listOfFiles = aFile.listFiles();
      if(listOfFiles!=null) {
        for (int i = 0; i < listOfFiles.length; i++)
          Process(listOfFiles[i]);
      } else {
        System.out.println(spcs + " [ACCESS DENIED]");
      }
    }
    spc_count--;
  }

  public static void main(String[] args) {
    String nam = "/";
    File aFile = new File(nam);
    Process(aFile);
  }

}
__________________
registered linux user #192405
Note : i am usually not available in this forum Tuesday-Friday.

My Desktop Smolt

My Notebook Smolt
Small amount of personal info and notes about Fedora
Notes about Fedora, Java, JOGL and NeHe OpenGL Tutorials
Reply With Quote
Reply

Tags
directories, files, folders, including, java, listing

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ls command not listing common directories tech291083 Using Fedora 3 12th May 2007 02:24 PM
Apache 2.2: Listing directories without index files... misjka Servers & Networking 1 24th September 2006 05:10 PM
Files not listing alphabetically from Win PC unclefalter Using Fedora 3 18th September 2005 09:12 AM
tar all files (including hidden files) leonstr Using Fedora 3 11th July 2005 10:46 PM
ls; more than just for listing files mongre26 Guides & Solutions (No Questions) 4 27th February 2004 10:20 PM


Current GMT-time: 06:53 (Saturday, 25-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat