• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

How to retrieve incoming files from a directory and verify their completeness

I am polling a directory for incoming files. I would like to find a way to verify that the file I am reading has already finished being written, i.e., is not currently in the process of being written to that directory, for example, in the middle of an FTP transfer. I cannot use a native solution - must be pure Java.

Thanks.
0
leeork
Asked:
leeork
  • 4
  • 3
  • 2
  • +1
1 Solution
 
RichardACommented:
I don't have specific code but if you find a file run a loop looking at the file size when the file size has stopped growing for s specific amount of time then you can assume that the file is complete.   The trick is how long to wait before assuming the file is there.  The File object has a length() method to return the size.

Not sure if there is any easier way...
0
 
RichardACommented:
Also checking for the File size should work for files being FTP'd but if you are on a windows machine (not sure aboue linux) and use a  file copy command to place the file in a directory the file size returned is the "completed" length not what is present at the time of the length() request so the file may not be completely there.

Here some a sample, if you keep running while a large file is being ftp'd you should see the file lenth change each time it is run.  Just change the path in the new File("C:/javawork") to your directory.

import java.io.*;
public class FileStuff {

    /** Creates new FileStuff */
    public FileStuff() {
    }

    /**
    * @param args the command line arguments
    */
    public static void main (String args[]) {
      File f = new File("C:/javawork");
      File l[] = f.listFiles();
      for(int i = 0; i < l.length;i++){
         if(l[i].isFile()){
            System.out.println("File - " + l[i].getName() + " - " + l[i].length());
         }
      }
   
    }
}
0
 
leeorkAuthor Commented:
Valid answer. I had already thought of that but as the connection is slow, and there may be temporary hangs during the transfer, it's hard to determine how long to wait before deciding the file will not arrive.

If there's no better way, I'll have to go that route.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
CEHJCommented:
Listening..
0
 
nhelliwellCommented:
Just a though.  If you open the file for writing, it'll throw an IOException because it should be locked by the operating system.
You could perform the same loop as above but try to open the file instead.  As soon as you can open it, the operating system will have finished writing to it.
0
 
leeorkAuthor Commented:
It turns out that when opening a FileInputStream on the file, an exception is thrown if another process is writing to that file - a FileNotFound exception is thrown with the following text:

"The process cannot access the file because it is being used by another process"

Thanks for the input though.
0
 
leeorkAuthor Commented:
Close enough. The only problem with opening a FileOutputStream (i.e. for writing) is that when the file is finally loaded, it's contents are erased as the output stream is ready to write to the beginning of the file. But opening a FileInputStream does work.

Thanks.
0
 
nhelliwellCommented:
If you do 'new FileOutputStream( <filename>, true )',  it should open the file to append to the end of the data in the file.

Actually there is a File.canWrite() method available have you tried that?
0
 
CEHJCommented:
If you're using 1.4 you could try one of the file locking methods. For instance, you won't be able to obtain a lock until a write has finished, so this should be pretty conclusive.
0
 
leeorkAuthor Commented:
I tried the canWrite - always returned true - I think that's a permission method.

Regarding jdk 1.4, if the powers that be give the go ahead, the file locking method would be the way to go.
0
 
CEHJCommented:
>>- I think that's a permission method.

You're right
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now