0

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.

arabian_albert
  • 708
  • 1
  • 11
  • 24
  • How about passing the File to generatePdf()? You could call getAbsolutePath() on the passed in file instead of the "inputFormML" variable. – stdunbar Feb 25 '16 at 17:51
  • `Files.walk()` will help – Alex Salauyou Feb 25 '16 at 17:52
  • The same question has been asked earlier. Below is the link for it http://stackoverflow.com/questions/1844688/read-all-files-in-a-folder – user3509208 Feb 25 '16 at 17:53
  • Guys my method getListOfFiles() already does this, I need help for my specific situation. How can I call generatePdf() the same amount of times of files in the directory – arabian_albert Feb 25 '16 at 18:01
  • Just run through the `File[]` with a `for` loop and call it as many times is necessary... – ifly6 Feb 25 '16 at 18:03
  • @ifly6 yes callGeneratePdf() which calls generatePdf() does this, that is where I am stuck. The return type of generatePdf() is byte, so I would have to change the type in run(). – arabian_albert Feb 25 '16 at 19:09
  • I think I probably didn't ask my question clearly enough. I'm gonna edit – arabian_albert Feb 25 '16 at 19:18

1 Answers1

0

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);
Benoit Vanalderweireldt
  • 2,925
  • 2
  • 21
  • 31