Export recursive listing directories

0

I'm doing a recursive directory tour and I want to export the directories and files found to an excel file. This is my code

package packages;

import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class hh {

    public void leer(String inicio, String altura) {
        File ar = new File(inicio);
        String[] dir = ar.list();
        XSSFWorkbook book = new XSSFWorkbook();
        XSSFSheet sheet = book.createSheet();
        for (int f = 0; f < dir.length; f++) {
            File ar2 = new File(inicio + dir[f]);
            String sss = "Directorio: " + dir[f];
            XSSFRow row = sheet.createRow(f);

            if (ar2.isFile()) {
                //System.out.println(altura + dir[f]);
            }
            if (ar2.isDirectory()) {
                System.out.println(altura + "Directorio:" + dir[f]);
                for (int j = 0; j < dir.length; j++) {
                    XSSFCell cell = row.createCell(j);
                    cell.setCellValue(sss);
                }
                leer(inicio + dir[f] + "\", altura + "  ");
            }

        }
        try {
            FileOutputStream out = new FileOutputStream(new File("C:\Users\Desktop\jdjdj.xlsx"));
            book.write(out);
            System.out.println("Excel written successfully..");
        } catch (Exception e) {
        }
    }

    public static void main(String[] arguments) {
        hh rec = new hh();
        rec.leer("C:\Users\Desktop\internacionales\", "");
    }
}

When the .xlsx file is generated, it only shows me the main folders, that is, the first folders that it finds in the path that you specify. I want to print all the subfolders in an excel file

    
asked by S.Bruce 08.04.2016 в 17:36
source

2 answers

1

The Apache Commons IO library has the FileUtils class that offers you a large number of useful methods to work with files and directories, in your case the < strong> public static Collection < File > listFilesAndDirs (File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) offers what you are looking for, the code would be:

Collection<File> directorios=FileUtils.listFilesAndDirs(new File(inicio), DirectoryFileFilter.DIRECTORY,TrueFileFilter.TRUE);

Here is a collection of Files that are all directories found recursively.

    
answered by 09.04.2016 в 02:22
0

The problem is that you are calling the method recursively and on each call you write the book in a physical output, denoted in these parts:

public void leer(String inicio, String altura) {
    //...
    XSSFWorkbook book = new XSSFWorkbook(); //<- creas el archivo Excel en memoria, local al método
    //..
    for (int f = 0; f < dir.length; f++) {
        //...
        if (ar2.isDirectory()) {
            //...
            leer(inicio + dir[f] + "\", altura + "  "); //<- llamada recursiva al método
        }
    }
    try {
        //...
        book.write(out); //<- escribes el libro Excel local al método en disco
        //...
    } //...
}

The best thing you can do is move the creation and writing of the Excel book out of the method, pass the variable that handles the Excel workbook as a parameter to the method and write the file after executing the method, like this:

public class hh {

    public void leer(String inicio, String altura, XSSFWorkbook book) {
        File ar = new File(inicio);
        String[] dir = ar.list();
        for (int f = 0; f < dir.length; f++) {
            File ar2 = new File(inicio + dir[f]);
            String sss = "Directorio: " + dir[f];
            XSSFSheet sheet = book.getSheetAt(0);
            XSSFRow row = sheet.createRow(sheet.getLastRowNum() + 1);
            if (ar2.isFile()) {
                //System.out.println(altura + dir[f]);
            }
            if (ar2.isDirectory()) {
                System.out.println(altura + "Directorio:" + dir[f]);
                for (int j = 0; j < dir.length; j++) {
                    XSSFCell cell = row.createCell(j);
                    cell.setCellValue(sss);
                }
                leer(inicio + dir[f] + "\", altura + "  ");
            }
        }
    }

    public static void main(String[] arguments) {
        hh rec = new hh();
        XSSFWorkbook book = new XSSFWorkbook();
        XSSFSheet sheet = book.createSheet();
        rec.leer("C:\Users\Desktop\internacionales\", "", book);
        try {
            FileOutputStream out = new FileOutputStream(new File("C:\Users\Desktop\jdjdj.xlsx"));
            book.write(out);
            System.out.println("Excel written successfully..");
            book.close();
        } catch (Exception e) {
        }
    }
}
    
answered by 08.04.2016 в 22:43