?
Solved

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

Posted on 2003-02-25
11
Medium Priority
?
266 Views
Last Modified: 2010-03-31
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
Comment
Question by:leeork
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 1

Expert Comment

by:RichardA
ID: 8019897
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
 
LVL 1

Expert Comment

by:RichardA
ID: 8020237
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
 

Author Comment

by:leeork
ID: 8020249
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 86

Expert Comment

by:CEHJ
ID: 8020263
Listening..
0
 

Accepted Solution

by:
nhelliwell earned 900 total points
ID: 8020424
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
 

Author Comment

by:leeork
ID: 8020517
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
 

Author Comment

by:leeork
ID: 8020562
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
 

Expert Comment

by:nhelliwell
ID: 8020634
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8020677
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
 

Author Comment

by:leeork
ID: 8021774
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8021810
>>- I think that's a permission method.

You're right
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question