[Last Call] Learn how to a build a cloud-first strategyRegister Now


putting the contents of multiple directories in a buffer

Posted on 2006-05-02
Medium Priority
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
  • 2
LVL 19

Expert Comment

by:Jim Cakalic
ID: 16589502
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(iter.next()).append(" ");

LVL 19

Accepted Solution

Jim Cakalic earned 500 total points
ID: 16589559
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

by:Mayank S
ID: 16593503
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:


http://www.javaalmanac.com/egs/java.io/GetFiles.html (non recursive)

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 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