1

I'm trying to make an applet that reads a file on the local file system (the users computer) at a very frequent interval (several times a second), then makes the contents of the file available to the web page via javascript.

The file the applet needs to read is updated at a high frequency by a program on the user's computer. What I'm concerned about is what might happen if the applet reads data from the file when the file is in the middle of being updated.

I don't know how likely this is, but if it is a valid concern is there a way to make sure the file is not currently being written to before reading it?

Nate
  • 26,164
  • 34
  • 130
  • 214
  • what i feel is that until you don't save the file disk will have older copy and it will read that older file. – kaysush Mar 12 '13 at 02:06
  • @SuKu - There's a program running on the user's computer that will overwriting the contents of the file at a high frequency. That's my concern. I'm wondering if the applet might read the file when it's in the middle of being overwritten. – Nate Mar 12 '13 at 02:11
  • you can read the file provided that you don't get an exception that file is already in use and you don't have any problem if for some reads you get the older contents. – kaysush Mar 12 '13 at 02:15
  • Does the file have text or data? (ASCII, unicode, binary)? – Jess Mar 12 '13 at 02:54
  • Do you have to use Java? Some users may have Java disabled in the browser due to security issues. – Jess Mar 12 '13 at 02:56

2 Answers2

2

I'm not positive about this, but you could try java.io.FileInputStream, or some other option from that package.

Also, this question may be a duplicate. This might answer your question:

  1. How do I use Java to read from a file that is actively being written?
  2. reading a file while it's being written
  3. Read a file while it's being written
  4. Reading data from a File while it is being written to
Community
  • 1
  • 1
Jess
  • 23,901
  • 21
  • 124
  • 145
1

its very monster to make such a disk access, any way try Sockets if you can or if again you sits back try to lock file in both ends if the one of the locking fails then make sure that other is locking ,make up this to your use

File file = new File(fileName);
FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
// Get an exclusive lock on the whole file
FileLock lock = channel.lock();
try {
    lock = channel.tryLock();
    // Ok. You get the lock
} catch (OverlappingFileLockException e) {
    // File is open by other end 
} finally {
    lock.release();
}
internals-in
  • 4,798
  • 2
  • 21
  • 38