Skip to main content
Announcements
July 15, NEW Customer Portal: Initial launch will improve how you submit Support Cases. READ MORE

How to check if a file is empty in a Job

No ratings
cancel
Showing results for 
Search instead for 
Did you mean: 
TalendSolutionExpert
Contributor II
Contributor II

How to check if a file is empty in a Job

Last Update:

Apr 10, 2023 11:51:18 PM

Updated By:

TalendSolutionExpert

Created date:

Apr 10, 2023 11:51:18 PM

When you are processing a file, you may have a logical need to only process it when it is not empty. You can check if a file is empty in two ways.

The first way is to hard code a solution via a tJava or tJavaRow. For example, check if  a file is empty on a tJava and put the result to a global variable.

java.io.File newFile = new java.io.File("D:/files/test.txt"); 
if (newFile.length() == 0) { 
System.out.println("File is empty ..."); 
globalMap.put("isEmpty", true);
} else {
 System.out.println("File is not empty ...");
globalMap.put("isEmpty", false);
  }


Another way is to use the tFileProperties component, which outputs the main properties of the processed file, where the size property is calculated as the size of the file in bytes. If it is 0, the file is empty.
For example...

tFileProperties--main-->tJavaRow--runIf--next processing on tJavaRow:

if(input_row.size==0){
globalMap.put("isEmpty", true);
}else{
globalMap.put("isEmpty", false);
}

0EM5b00000ArZNV.png

As shown in the above code and screenshot, we usually put the result to global variable for use later. You can use the global variable as the condition of a RunIf link to decide whether it needs to trigger the next processing.
 
Contributors
Version history
Last update:
‎2023-04-10 11:51 PM
Updated by: