Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print directory tree

Tags:

java

algorithm

I have to print a directory tree (like tree command), example:

 .
 +---A
 |   +---IMAGES
 |       +---BACKUP
 +---ADOKS
 |   +---ROZDZIAL_2
 |   +---ROZDZIAL_3
 |   +---ROZDZIAL_4
 +---AMSC2005
 |   +---AMSC2004
 +---FCCS2005
 |   +---source
 |   +---TMP
 +---LODZ2004
 +---ZAKOPANE2004
 +---DYDAKTYKA
 |   +---DYDAKTYKA_ISI
 |   |   +---COLLS
 |   |   |   +---Q1
 |   |   |   +---Q2
 |   |   |   +---RAZEM
 |   |   |       +---RYSUNKI_COLL1
 |   |   |       +---RYSUNKI_COLL2
 |   |   +---IMAGES
 |   |   +---src
 |   |   +---WYKLAD5
 |   +---DYDAKTYKA_PRG
 |       +---images
 |       +---POMOC
 +---DYDAKTYKA_KST
 |   +---images
 |   +---src
 +---DYDAKTYKA_WPR
     +---images
     +---src

I tried with the following Code:

private static void getInto(String p, int n) {
    File path = new File(p);
    File[] list = path.listFiles();

    for (int i = 0; i < list.length; i++) {
        if (list[i].isDirectorhowny()) {
            for (int j = 0; j < n; j++)
                if (WHAT HERE?)
                    System.out.print("|  ");
                else
                    System.out.print("   ");

            System.out.println("+--" + list[i].getName().toString());

            getInto(list[i].getPath(), n + 1);
        }
    }
}

I tried few version, but it's still not work. How to do this? What condition should be put in If? I know is pretty simple, but I can't do this.

like image 446
user1248092 Avatar asked May 18 '12 15:05

user1248092


2 Answers

This is My Code. its Work

package DIRS;

import java.io.File;

public class LISTDIR
{
    public static void main(String[] args)
    {
        String path = System.getProperty("user.dir")+File.separator;

        File file[] = new File(path).listFiles();

        for(int i = 0 ; i < file.length ; i++)
        {
            if(file[i].isDirectory())
            {
                System.out.println("->"+file[i].getName());
                new LISTDIR().listfiles(file[i],"--");
            }
            else
            {
                System.out.println("-> "+file[i].getName());
            }
        }
    }

    public void listfiles(File f,String sprtr)
    {
        File file[] = f.listFiles();
        for(int i = 0 ; i < file.length ; i++)
        {
            if(file[i].isDirectory())
            {
                System.out.println(sprtr+file[i].getName());

                new LISTDIR().listfiles(file[i],"--"+sprtr);
            }
            else
            {
                System.out.println(sprtr+file[i].getName());
            }
        }
    }
}
like image 61
13hola Avatar answered Oct 10 '22 17:10

13hola


This is how I did it.

The Code

import java.io.File;
public class FileAssert {

/**
 * Pretty print the directory tree and its file names.
 * 
 * @param folder
 *            must be a folder.
 * @return
 */
public static String printDirectoryTree(File folder) {
    if (!folder.isDirectory()) {
        throw new IllegalArgumentException("folder is not a Directory");
    }
    int indent = 0;
    StringBuilder sb = new StringBuilder();
    printDirectoryTree(folder, indent, sb);
    return sb.toString();
}

private static void printDirectoryTree(File folder, int indent,
        StringBuilder sb) {
    if (!folder.isDirectory()) {
        throw new IllegalArgumentException("folder is not a Directory");
    }
    sb.append(getIndentString(indent));
    sb.append("+--");
    sb.append(folder.getName());
    sb.append("/");
    sb.append("\n");
    for (File file : folder.listFiles()) {
        if (file.isDirectory()) {
            printDirectoryTree(file, indent + 1, sb);
        } else {
            printFile(file, indent + 1, sb);
        }
    }

}

private static void printFile(File file, int indent, StringBuilder sb) {
    sb.append(getIndentString(indent));
    sb.append("+--");
    sb.append(file.getName());
    sb.append("\n");
}

private static String getIndentString(int indent) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < indent; i++) {
        sb.append("|  ");
    }
    return sb.toString();
}
}

The Result

+--folder1/
|  +--a.txt
|  +--folder2/
|  |  +--b1.txt
|  |  +--b2.txt
|  |  +--b3.txt
|  |  +--folder3/
|  +--folder4/

The UnitTest

import static org.junit.Assert.*;

import java.io.File;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class FileAssertTest {

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
private File folder1;

@Before
public void setUp() {
    folder1 = temporaryFolder.newFolder("folder1");
}

@Test
public void testPrintDirectoryTreeWhenFolderIsEmpty() {
    // Invoke
    String actual = FileAssert.printDirectoryTree(folder1);
    // Verify
    assertEquals("+--folder1/\n", actual);
}

private static final String EXPECTED_FCOF = "" + "+--folder1/\n"
        + "|  +--a.txt\n";

@Test
public void testPrintDirectoryTreeWhenFolderContainsOneFile()
        throws Exception {
    // Setup
    File aFile = new File(folder1, "a.txt");
    assertTrue(aFile.createNewFile());
    // Invoke
    String actual = FileAssert.printDirectoryTree(folder1);
    // Verify
    assertEquals(EXPECTED_FCOF, actual);
}

private static final String EXPECTED_COMPLEX = "+--folder1/\n"
        + "|  +--a.txt\n" + "|  +--folder2/\n" + "|  |  +--b1.txt\n"
        + "|  |  +--b2.txt\n" + "|  |  +--b3.txt\n" + "|  |  +--folder3/\n"
        + "|  +--folder4/\n";

@Test
public void testPrintDirectoryTreeComplex() throws Exception {
    // Setup
    File aFile = new File(folder1, "a.txt");
    assertTrue(aFile.createNewFile());
    File folder2 = new File(folder1, "folder2");
    assertTrue(folder2.mkdir());
    File b1File = new File(folder2, "b1.txt");
    assertTrue(b1File.createNewFile());
    File b2File = new File(folder2, "b2.txt");
    assertTrue(b2File.createNewFile());
    File folder3 = new File(folder2, "folder3");
    assertTrue(folder3.mkdir());
    File b3File = new File(folder2, "b3.txt");
    assertTrue(b3File.createNewFile());
    File folder4 = new File(folder1, "folder4");
    assertTrue(folder4.mkdir());

    // Invoke
    String actual = FileAssert.printDirectoryTree(folder1);
    // Verify
    assertEquals(EXPECTED_COMPLEX, actual);
}

}

file directory printing pretty-printing

like image 42
TAN70 Avatar answered Oct 10 '22 17:10

TAN70