Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
hello ,
I'm trying to create a list of files that are opened by user .
Here is my job design
to check if a file is open i used this code
File sourceFile = new File(((String)globalMap.get("tFileList_3_CURRENT_FILEPATH")).toString());
List<String> file_Name_Opend=new ArrayList<String>();
if(!sourceFile.renameTo(sourceFile)) {
file_Name_Opend.add(sourceFile.toString()) ;
System.out.println("** Files opened are :"+ file_Name_Opend);
}
But my list is always overwritten and got always the last file that is opened .
How to resolve this ?
You are nearly there, but you are overlooking the fact that the tJava2 is run in its entirety for every iteration. This means that you are recreating your ArrayList for every file. This is why the data is overwritten.
I was round this is to use the globalMap HashMap. I have converted your code above to use this and built a demo job to test it.
My job looks like this.....
It's essentially the same as yours, except I have added a tJava below to iterate over the data collected to test my code.
The code which updates yours is below......
*****************************************************************************************************
java.util.ArrayList<String> file_Name_Opend = null;
//Check to see if an ArrayList already exists in the globalMap. If not, create it.
if(globalMap.get("myFileList")==null){
file_Name_Opend=new java.util.ArrayList<String>();
}else{
file_Name_Opend = ((java.util.ArrayList)globalMap.get("myFileList"));
}
String sourceFile = ((String)globalMap.get("tFileList_1_CURRENT_FILEPATH"));
file_Name_Opend.add(sourceFile) ;
globalMap.put("myFileList", file_Name_Opend);
*****************************************************************************************************
My code in the tJava below to test what is collected is seen below.........
*****************************************************************************************************
java.util.ArrayList<String> file_Name_Opend = ((java.util.ArrayList)globalMap.get("myFileList"));
java.util.Iterator<String> it = file_Name_Opend.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
*****************************************************************************************************
Yes try tjavaflex instead to do this.
The tJavaFlex won't solve the problem here. On a flow link it would, but this is an iterate link. As such the Start and End sections of the tJavaFlex will be triggered for every iteration as well. However, it is certainly the way to go in most situations.
tFileList ---iterate----> tIterateToFlow -----row----> tjavaflex
Yes, that should work. The code would look something like below I think.....
Start Code
*****************************************************************************************************
java.util.ArrayList<String> file_Name_Opend = new java.util.ArrayList<String>();
*****************************************************************************************************
Main Code
*****************************************************************************************************
file_Name_Opend.add(row.column) ;
*****************************************************************************************************
End Code - to save the ArrayList object for use later in the job
*****************************************************************************************************
globalMap.put("myFileList", file_Name_Opend);
*****************************************************************************************************
Thanks for the suggestion, it helps to give as many solutions as possible 🙂