Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Bucharest on Sept 18th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
SnowFrog
Contributor
Contributor

FileInputStream.close() in a tJavaRow is not closing the file

Hi there

Context.

I receive files from an MQ server. the next step is to read part of each file to determine the file type based on its content.

So I do a tListFile which iterate through the file and on each does a jJavaRow

in the tJavaRow the file is open, 255 bytes are read and the file closed immediately even before the actual parsing happens. This actually works file

But further down the pipe I try to move the file with a tFileCopy but I get an error "The source File "20230517_000633.001" does not exist or is not a file."

To ensure that the issue is really with the FileInputStream close() I commented out the opening of the file and the file read and I do not get the error and the file is moved.

This is the code snippet where the file is open, read and immediately closed. That catch on the close is not triggered and I do get the "attempting to formally close the file" message

Any Idea why this code would not release the resource. It seems like this maybe talend related because this is the recommended way to close a FileInputStream.

I even tried to formally call the System.gc() to force the release but nothing

Any Ideas?

    try {

      FileInputStream fileinputstream = new FileInputStream(fileName);

      byte[] b = new byte[255];

      n = fileinputstream.read(b);

       

        

      try{

       if (fileinputstream != null) fileinputstream.close();

        System.out.println("Attempting to formally close the File");

      } catch (IOException e) {

       System.out.println("Could not close the file, try-catch triggered");

     }

       

Labels (3)
4 Replies
Anonymous
Not applicable

Hello @Chris Bolduc​ ,

Regarding to your above java code, it may not close the fileinputstream while reading the file content exception. the best way is to use try...catch...finally code as the below, also, please make sure the File Name path is an absolute existing file path in tFileCopy component

 

    FileInputStream fileinputstream = null;

    try {

 

      fileinputstream = new FileInputStream("");

      byte[] b = new byte[255];

      int n = fileinputstream.read(b);       

    } catch (IOException e) {

      System.out.println("Could not close the file, try-catch triggered");

    } finally {       

      try {

        if (fileinputstream != null) fileinputstream.close();

      } catch (IOException ex) {

      }

    }

SnowFrog
Contributor
Contributor
Author

Thank you @Aiming Chen​  I did have something like this but I modified it to shrink the code for posting.

Now I've actually change the code to the following code and I still get the very same results.

 

Any other suggestion?

 

    try {

       

      fileinputstream = new FileInputStream(fileName);

      n = fileinputstream.read(b);

       

    } catch (IOException e){

      

     process_status += ": \tCould not open/find file in processing directory"; 

    

     talendLogs_LOGS.addMessage("tJavaRow", "Extract_Info", 5, process_status, context.CodeLevel + 50);

     talendLogs_LOGSProcess(globalMap); 

         

    } finally {

     try{

       if (fileinputstream != null) {

         fileinputstream.close();

         System.out.println("File was closed properly");

         fileinputstream = null;

       } else {

        System.out.println("File pointer is null - file could not be closed properly");

       }

      } catch (IOException e) {

       System.out.println("Could not close the file, try-catch triggered");

      }

    } 

     

Anonymous
Not applicable

@Chris Bolduc​ ,

Regarding to the error log "The source File "20230517_000633.001" does not exist or is not a file."

seems the problem is that the file name path is not correct.

please make sure the File Name path is an absolute existing file path in tFileCopy component?

SnowFrog
Contributor
Contributor
Author

thank you @Aiming Chen​  I will confirm.