I am working on a project that involves coding in java. I'm facing a problem in it right now that I need to check if a specific file is currently open. I'll do some specific actions accordingly. I know that Check if file is already open this post seems to be a duplicate of my problem. However, none of the methods specified in it seem to work for .txt files, while I want it just for these ones. Any help would be appreciated. Thank You!
Asked
Active
Viewed 53 times
0
-
Why they are not working for .txt files? – ZhaoGang Dec 15 '18 at 12:29
-
@ZhaoGang I don't know! My file is open, but it still shows that it's closed – Dec 15 '18 at 12:30
-
1) which operating system? 2) what do you mean by "open" - there's open for reading, for writing, different exclusion modes, file locks, and all kinds of operating system specific things – Joni Dec 15 '18 at 12:38
-
@Joni I'm on Windows 10 and by open, I mean visible in the taskbar (I hope you know what I mean) – Dec 15 '18 at 12:40
-
2What application has the file open? Chances are the file is actually closed on the operating system level – Joni Dec 15 '18 at 12:41
-
Its a .txt file, open in notepad. I can either view or write some stuff to it – Dec 15 '18 at 12:42
-
https://drive.google.com/open?id=1sOZhWm0ccRs5frQ5h1Mubl2YEe4fH5-8 It's a drive link to screenshot of my laptop screen when the code runs – Dec 15 '18 at 12:45
-
The solution provided at the link you found is probaly the best you'll get, since having a file "open" in some application doesn't mean that the file is currently open in a detectable way, since many programs work wit a temorary copy or somtimes even just the file's contens in RAM. So the file itself seems to be closed in every way you can check for. – Poohl Dec 15 '18 at 12:45
-
2This could be [an X-Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What do you actually want to do? – Robin Green Dec 15 '18 at 12:58
-
@RobinGreen I'm writing a code for a kind-of cloud service provider. The user's operations include editing and deleting a file, but I don't want him to run the delete command while he's still editing the file (or in this case, the file is still open). Further, I'm not the one giving any of these commands. they are being randomly given, probabilistically, using multithreading, and I stop the execution of the current user's thread until he closes the particular file. The only problem I'm facing in is notifying the thread because I'm unable to know when has the user closed that particular file – Dec 15 '18 at 13:01
-
Then Notepad is irrelevant - unless you're intending to allow users to run Notepad over the internet? You only need to care about the applications you actually allow the users to run. – Robin Green Dec 15 '18 at 13:05
-
Ya I know. I just ran into this problem while I was testing the same on my laptop and am wondering how could it be fixed – Dec 15 '18 at 13:07
-
2notepad doesn't lock files, hence you won't be able to use it to test/develop that chuck of code. In a nutshell, when multiple actors can modify the same resource, you're going to run into problems. Consider looking into concurrency design patterns: https://en.wikipedia.org/wiki/Concurrency_pattern – Emmanuel Rosa Dec 15 '18 at 13:11
-
There may be a way to get the list of open notepad.exe windows specifically and ask them which files they have open - but it sounds like fixating on notepad.exe is not going to help you – Joni Dec 15 '18 at 13:17