Copying files using apache fileutil.copyfile

I am using apache util to copy a file to a directory using the fileutil.copyFile(src, dest)

The file I am copying is updated every 2 seconds by an external vendor and I really do not want to lock it (my application is running in windows- this will cause all sorts of problems). I am hoping someone might be able to help me in advising what is the safest way to copy or even read a file without the source file getting locked?

Kind regards


Because you are not explicitly locking the entire file before the copy action the default os file locking mechanism is at work.

I ran a quick test program to see what happens on a Windows machine when you copy a source file while an external process is writing to the file every 2secs.

The process that writes to the file never encountered a problem.

public static void main(String[] args) {
    File f = new File("..test.txt");
    long startTime = System.currentTimeMillis();
    long elapsedTime = 0;
    while (elapsedTime < 1000 * 60) {
        try {
            FileUtils.writeStringToFile(f, System.currentTimeMillis()+" : Data Writern", true);
            Thread.sleep(2000);
        } catch (IOException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex){
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
        elapsedTime = System.currentTimeMillis() - startTime;
    }
}

The process that copies the file will throw an exception if it does not finish the copy before the source file changes length. It appears that this exception is more of a warning that the copied version of the file is incomplete. When I synced timing to keep from reading from the file at the same time writing happens this exception was not thrown.

public static void main(String[] args) {
    File f = new File("..test.txt");
    long startTime = System.currentTimeMillis();
    long elapsedTime = 0;
    while (elapsedTime < 1000 * 60) {
        try {
            FileUtils.copyFile(f, new File("..testtest.txt"));
            Thread.sleep(2000);
        } catch (IOException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex){
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
        elapsedTime = System.currentTimeMillis() - startTime;
    }
}

Based on this test I would not worry about what happens to the writing process. I would do something to handle the case when java.io.IOException: Failed to copy full contents from '..test.txt' to '..testtest.txt' is thrown.


This is an excerpt from How to Cache a File in Java, more can be found there.

Caching Files in Java


Reading files from the disk can be slow, especially when an application reads the same file many times. Caching solves this problem by keeping frequently accessed files in memory. This allows the application to read the content of the from the fast local memory instead of the slow hard drive. Design for caching a file in Java includes three elements:

  • An algorithm for caching the file
  • A data structure for holding the cached content
  • A cache API for storing cached files
  • Algorithm for Caching Files


    A general algorithm for caching a file must account for file modifications and consists of the following steps:

  • Get a value from the cache using a fully qualified file path as a key.
  • If a key is not found, read the file content and put it to the cache.
  • If the key is found, check if a timestamp of the cached content matches the file timestamp.
  • If the timestamps are equal, return the cached content.
  • If the timestamps are not equal, refresh the cache by reading the file and putting it into the cache.

  • Have a look at http://docs.oracle.com/javase/6/docs/api/java/io/RandomAccessFile.html.

    Maybe this will help you.

    链接地址: http://www.djcxy.com/p/12404.html

    上一篇: 在JSF中创建自定义严重性消息

    下一篇: 使用apache fileutil.copyfile复制文件