简体   繁体   中英

Reading Multiple files processing text and generating report in JAVA

I'm currently working on a framework wherein we do perform some java execution, as of now we have to manually write the java code and execute it. To make life simpler for us we are trying to generate a framework wherein the user will write what he has to execute in a text file in plain English, for example - Sum | 2+3 Sum | 2+3 . Now the definition of these keyworks like Sum, Divide, Product etc. we have already defined in our java class file. We now read all these text files from a folder 1 by 1 and rest execution is done by our code accordingly.

We have already implemented the thing mentioned above, however now after execution we want to generate a report like following - Lets say if there were 5 text files in the folder namely (File number will be dynamic) -

  • 001
  • 002
  • 003
  • 004
  • 005

Out of these, whatever code was executed as part of 004, threw some error but rest all worked fine.

We want to have an excel report which displays the report as -

  • 001 - Passed
  • 002 - Passed
  • 003 - Passed
  • 004 - Failed - Stacktrace
  • 005 - Passed

How is this possible in JAVA? We are using buffered file reader to read file contents and then are splitting the string with "|" char and then matching/processing the keyword accordingly

Since you haven't added any code I can't be more specific but you can do something like this:

try(BufferedWriter bw = new BufferedWriter(new FileWriter("report.txt"))){
    for(File currentFile : filesToProcess){
        try{
            processor.processFile(currentFile);
            bw.write(currentFile.getName() + " - Passed\n" );
        }catch(Exception e){
            bw.write(currentFile.getName() + " - Failed " + e.getMessage() + "\n");
        }
    }
}catch(IOException e){
        e.printStackTrace();
}

In essence, wrap your code for processing the content of the files into a method and add lines to a log files based on if that method was successfully executed or it produced a runtime error.

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