putting the contents of multiple directories in a buffer

Posted on 2006-05-02
Last Modified: 2010-03-31
top level directory name is topdir.and 3 sub directories and then sub sub directories for example

sub1 - dir1,, dir2, dir3
sub2- dir1, dir2, dir3
sub3 - dir1, dir2, dir3

what  i have been trying to do is to read the last set of sub directores and put them all in one array. alldir[] = dir1, dir2,dir3,dir1,dir2,dir3,dir1,dir2,dir3

I created an stringBuffer to store them but only prints giberish after a split.
code beleow
String mine[];
stringBuffer temp = new stringBuffer();
File dir = new File(topdir);
 allDir = dir.list();
 for (int dircnt = 0; dircnt < allDir.length; dircnt++) {
  String newPath = topdir + "\\" + allDir[dircnt];
  File subDir = new File(newPath);
 temp.append.(" ");
mine = new String[cnt];
String newstr = buffer.toString();
mine = split.newstr(" ");

would this be the correct way to get all in the same array?


Question by:tfbsr
    LVL 19

    Expert Comment

    by:Jim Cakalic
    There isn't a StringBuffer.append(List). The StringBuffer.append(Object) appends the toString of the supplied Object which, for a List, is probably now what you are expecting. It might be better to loop on the List of filenames returned and append individually:

        File subDir = new File(newPath);
        for (Iterator iter = subDir.list().iterator(); iter.hasNext(); ) {
            temp.append(" ");

    LVL 19

    Accepted Solution

    Heh. Stupid. File.list() doesn't return a List. How about:

        File subDir = new File(newPath);
        String[] files = subDir.list();
        for (int i = 0; i < files.length; ++i) {
            temp.append(files[i]).append(" ");
    LVL 30

    Expert Comment

    You can use this to iterate through the directory and child directories recursively if that's the intention. Append them in a StringBuffer () in the process () method as Jim as shown: (non recursive)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One Java Help from Top Experts

    Solve your toughest problems, fast.
    Java experts are online now and ready to help you.

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    The viewer will learn how to implement Singleton Design Pattern in Java.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now