简体   繁体   中英

How to read from a directory of files rather than just 1 file Java

I'd like to read from a directory of files rather than just 1 file Java, I've got most of this setup, I'm just stuck as how to make it happen. I want to call generatePdf() for every file in a directory

Here is my code:

I'd like to read from a directory of files rather than just 1 file Java, I've got most of this setup, I'm just stuck as how to make it happen. I want to call generatePdf() for every file in a directory

Here is my code:

private void run() throws Exception 
{
   // This call here
   byte[] pdfBytes = this.generatePdf();

   I was thinking I could do something like this

   // some more processing

}

private void callGeneratePdf(File[] listOfFiles) 
{

    listOfFiles = getListOfFiles(DEFAULT_FORMML_FILE);

    for(File f : listOfFiles) 
    {
        f = generatePdf(); // ??? 
    }
}

private byte[] generatePdf() throws Exception
{
    String appPrintList = "<PrintListML/>"; // Empty application specific print list; no record worksheets will be included.

    String formML = PrintEngine.readFileAsString(inputFormML.getAbsolutePath());

    PrintListOptions plOptions = new PrintListOptions().setIncludeRecords(true).setRequireFormMLStatusPOR(true).setRequireFormMLStatusActive(false);
    PrintOptions pOptions = new PrintOptions().setItemizations(true).setSmartWorksheets(true);

    // Generate the PDF
    return FpsService.getPrintJob(formML, appPrintList, plOptions, pOptions);       
}



private static File[] getListOfFiles(String path) 
{

    String files;
    File folder = new File(path);
    File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++) {

        if (listOfFiles[i].isFile()) {
            files = listOfFiles[i].getName();
            System.out.println(files);
        }
    }

    return listOfFiles;
}

My plan was to write a new method that calls generatePdf() as many times there is files in the File array (File[]). Any help or suggestions would be much appreciated, thanks.

My plan was to write a new method that calls generatePdf() as many times there is files in the File array (File[]). Any help or suggestions would be much appreciated, thanks.

What about using a special implementation of the SimpleFileVisitor where you would call your logic every time you visit a file ?

final FileVisitor<Path> myWalker = new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        if(Files.isRegularFile(file, LinkOption.NOFOLLOW_LINKS)){
            //Call f = generatePdf()
        }
        return super.visitFile(file, attrs);
    }
};
Files.walkFileTree(Paths.get("/yourpath/"), myWalker);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM