filelock

Java: opening and reading from a file without locking it

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 11:09:33
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 fp = null; try{ fp = new Scanner(new FileReader(this.filename)); fp.useDelimiter("\n"); }catch(java.io.FileNotFoundException e){ System.out.println("java.io.FileNotFoundException e"); } while(true){ if(fp.hasNext()){ this.parse(fp.next()); } } } Rebuilding tail is

Can I prevent a StreamReader from locking a text file whilst it is in use?

巧了我就是萌 提交于 2019-11-28 08:18:50
The StreamReader locks a text file whilst it is reading it. 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: I've written a small app to get some stats out of a log file. This file is constantly being updating (several times a second) by an outside program lets call AAXXYY. Reviewing the output suggests that my app may be locking the file and preventing AAXXYY from writing. This is

Java FileLock for Reading and Writing

[亡魂溺海] 提交于 2019-11-28 07:27:31
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) write/append to this same data file. I want these two processes to play nice and only access the file one at a time. The nio FileLock seemed to be what I needed (short of writing my own semaphore type files), but I'm having trouble locking it for reading. I can lock

Problem with Java file locking mechanism (FileLock etc)

喜夏-厌秋 提交于 2019-11-28 01:07:13
问题 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 void readFile(){ File xmlFile = new File("myFile.xml"); RandomAccessFile raf = new RandomAccessFile(xmlFile, "rw"); FileLock fl = raf.getChannel().tryLock(); if(fl

How to synchronize file access in a Java servlet?

倖福魔咒の 提交于 2019-11-27 22:38:46
问题 I created a small Java servlet for a simple purpose: Once it is called, it will do the following steps: Read file foo.json from the local filesystem Process the data from the file and do some changes to it Write back the changes to the file Simplified version of the code: @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { FileInputStream inputStream = new FileInputStream("foo.json"); String filecontent = IOUtils

How to unlock a file from someone else in Team Foundation Server

心不动则不痛 提交于 2019-11-27 11:16:51
We have a project that is stored within our TFS server and some files were Checked-Out by me from another computer and another user (both of which are not used anymore). Is there a way to force the unlocking of the file (no changes were made to it so it's safe to do so if I can only do it). If you login into the source control with the admin account, you will be able to force undo checkout, or check in with any file you provide. Here's what I do in Visual Studio 2012 (Note: I have the TFS Power Tools installed so if you don't see the described options you may need to install them. http:/

Java: opening and reading from a file without locking it

拜拜、爱过 提交于 2019-11-27 05:57:22
问题 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 fp = null; try{ fp = new Scanner(new FileReader(this.filename)); fp.useDelimiter("\n"); }catch(java.io.FileNotFoundException e){ System.out.println("java.io

Can I prevent a StreamReader from locking a text file whilst it is in use?

半城伤御伤魂 提交于 2019-11-27 02:08:25
问题 The StreamReader locks a text file whilst it is reading it. 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: I've written a small app to get some stats out of a log file. This file is constantly being updating (several times a second) by an outside program lets call AAXXYY.

Java FileLock for Reading and Writing

非 Y 不嫁゛ 提交于 2019-11-27 01:48:43
问题 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) write/append to this same data file. I want these two processes to play nice and only access the file one at a time. The nio FileLock seemed to be what I needed

How to unlock a file from someone else in Team Foundation Server

别等时光非礼了梦想. 提交于 2019-11-26 22:20:27
问题 We have a project that is stored within our TFS server and some files were Checked-Out by me from another computer and another user (both of which are not used anymore). Is there a way to force the unlocking of the file (no changes were made to it so it's safe to do so if I can only do it). 回答1: If you login into the source control with the admin account, you will be able to force undo checkout, or check in with any file you provide. 回答2: Here's what I do in Visual Studio 2012 (Note: I have