filelock

When using Java's FileLock, is it ok to let close() to automatically do a lock.release()?

As most should know close() also closes any streams uses. This allows the follow code: BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(...))); ... br.close(); This is nice, since we don't need a reference to FileInputStream and remember to close it. But does it also work for FileLocks? final FileInp...

how to best wait for a filelock to release

I have an application where i sometimes need to read from file being written to and as a result being locked. As I have understood from other questions i should catch the IOException and retry until i can read. But my question is how do i know for certain that the file is locked and that it is not another IOExcetpion that occurs. ...

Is there a way to delete an output file in a post-build event.

I'm trying to run a post-build batch file on a .NET build that encrypts an output file, deletes the original and then renames the encrypted version to the original output filename. i.e.: Build A, then in post-build: Encrypt A->B DEL /F A RENAME B A I can't seem to delete the original output file after encryption though as it seems lik...

How to find out what processes have folder or file locked?

How do you go about querying running processes to find out what folders or files they have locked? (i.e. you go to eject a drive and you're told that it can't be ejected because it's in use) I'd like to either get an "off the shelf" download, or write a .NET 3.5 app to do this (primarily a Windows question). ...

How to prevent file from being overridden when reading and processing it with Java?

Hi, I'd need to read and process somewhat large file with Java and I'd like to know, if there is some sensible way to protect the file that it wouldn't be overwritten by other processes while I'm reading & processing it? That is, some way to make it read-only, keep it "open" or something... This would be done in Windows environment. ...

Java file locking and Windows - the lock isn't "absolute"?

Hi, I'm trying to lock a file with Java in Windows environment with FileLock and I got an issue : after I lock the file it can still be accessed by other processes at least on some level. Example code follows: public class SimpleLockExample { public static void main(String[] args) throws Exception { String filename = "loremli...

How do I get WPF's DocumentViewer to release its file lock on the source XPS Document?

After showing an XPS file in the WPF DocumentViewer, and closing the DocumentViewer instance, the XPS file is locked and I cannot delete it. I need to release the lock on the XPS file so I can delete it, write another one with the same name, and optionally display that new XPS file in a new DocumentViewer instance. I need to do this in...

Does a StreamReader lock a text file whilst it is in use? Can I prevent this?

Does a StreamReader lock a text file whilst it is reading it? If it does, can I force the StreamReader to work in a "read-only" or "non locking" mode? My workaround would be to copy the file to a temp location and read it from there but I would prefer to use the StreamReader directly if possible. Any alternative suggetions? Background:...

file lock leases via NFS v4 in C

does anybody know how to use the fancy file locking features of NFS v4? (described in e.g. About the NFS protocol (scroll down)). supposedly NFS v4 supports file lock leasing with a 45 second lifetime. I would like to believe that the linux kernel (I'm using gentoo 2.6.30) happily takes care of these details, and I can use fcntl() and it...

C# - Lock file until browser is closed, user navigates away from page, or session expires

Is there any way to lock a file until a browser is closed, the user leaves the current page, or their session expires? I have a java app that reads annotations from a text file and lets a user modify or add more annotations to a pdf through the java app. Once they do, they click 'save' and the full annotation file is returned to it's or...

Java FileLock for Reading and Writing

I have a process that will be called rather frequently from cron to read a file that has certain move related commands in it. My process needs to read and write to this data file - and keep it locked to prevent other processes from touching it during this time. A completely separate process can be executed by a user to (potential) writ...

How can my Linux daemon know when a Windows program has stopped writing a file that I access through SAMBA?

I'm developing a system that interfaces with a USPS shipping package called Dazzle. Part of this system includes a monitoring daemon whose purpose is to take tab-separated value files, turn them into XML that Dazzle recognizes, and pass them to Dazzle for label generation. And this part works just fine. What I also want to do, however...

.NET isolated storage file locking throws NRE

So I am trying to lock an isolated storage file in my C# client application, so that multiple copies of my application are not able to access it at the same time. I am using the following syntax: lockStream = new IsolatedStorageFileStream("my.lck", FileMode.OpenOrCreate, isoStore); lockStream.Lock(0, 0); This code causes my applicatio...

Problem with Java file locking mechanism (FileLock etc)

I am creating a simple application for opening and editing xml files. These files are located in a local folder accessed by multiple instances of the application. What I want to do is lock each file that is opened by an instance of the app, so that other instances cannot access it. To achieve this I use the following code: function voi...

Java: opening and reading from a file without locking it.

I need to be able to mimic 'tail -f' with Java. I'm trying to read a log file as it's being written by another process, but when I open the file to read it, it locks the file and the other process can't write to it anymore. Any help would be greatly appreciated! Here is the code that I'm using currently: public void read(){ Scanner...

Safest way to copy a file

Hello, I need to merg two PDF files. However sometimes a file might be locked up I wrote this code, but I'm wondering if it's not the smartest solution: private static int FILE_LOCKED_WAIT_PERIOD = 1000; while (true) { // If the file is in use, IOException will be thrown. ...

FileStream to save file then immediately unlock in .NET ?

I have this code that saves a pdf file. FileStream fs = new FileStream(SaveLocation, FileMode.Create); fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length); fs.Flush(); fs.Close(); It works fine. However sometimes it does not release the lock right away and that causes file locking exceptions with functions run after this on...

Cannot delete an image file that is shown in a listview

Hello all, In my listview I show thumbnails of small images in a certain folder. I setup the listview as follows: var imageList = new ImageList(); foreach (var fileInfo in dir.GetFiles()) { try { var image = Image.FromFile(fileInfo.FullName); imageList.Images.Add(image); } catch { Console.Wri...

In Java What is the guaranteed way to get a FileLock from FileChannel while accessing a RandomAccessFile ?

I am trying to use FileLock lock(long position, long size,boolean shared) in FileChannel object As per the javadoc it can throw OverlappingFileLockException. When I create a test program with 2 threads lock method seems to be waiting to acquire the lock (both exclusive and non exclusive) But when the number threads increases in the a...

Windows Java File lock when referencing existing file in constructor?

Suppose I do the following in java for a process that stays open: import java.io.File; import java.util.Date; public class LogHolder { public static void main(String[] args) { File file1 = new File("myLogFile.log"); while (true) { System.out.println("Running " + new Date()); } } } Ha...