Files do not need to be closed. As long as you do not maintain references to them, they will be GCd, as they go beyond.
The string if (i != 0) will always be evaluated as true, since you increment the variable i at least once before pushing this condition. Thus, firstSheet is always false.
Line
File inputFile = new File("C:\\Users\\edennis.AD\\Desktop\\test\\"+nameOfFile);
creates new files. However, you already have an object file for this path represented by child
You always write to the same file that you recreate for the file object and the new FileOutputStream for each time you iterate over the source directories, even if all the entries belong to the same file.
You do not close your FileOutputStream in the finally block, and it may not be right to close the FileOutputStream under error conditions.
Use StringBuilder instead of StringBuffer if you don't need synchronized methods to build a string.
Consider using FileWriter instead of an intermediate StringBuilder. Instead of writing in Builder, use
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(outputFile, true))))
Instead of using data.append use writer.print or writer.println The PrintWriter and Buffered Writer wrappers are not strictly necessary, but useful.
If you refer to the javadocs XSSFWorkbook for constructor options, you will see that βUsing InputStream requires more memory than using a file, so if the file is available, you should instead do something likeβ example follow β http: // poi .apache.org / apidocs / org / apache / poi / xssf / usermodel / XSSFWorkbook.html # XSSFWorkbook (java.io.InputStream)
Increasing the size of your heap is likely to be a workable solution if all else fails. Assuming you have no potential for significantly larger files than the ones you are currently testing with. Increase heap size in Java
Deadron
source share