473,769 Members | 6,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

To open file for writing only if it does not already exist

Is there a way to open file for writing so that the opening
fails if the file already exist? This needs to be atomic
operation so that it is possible to use the file as a
semaphore. As a result, the following coding does not do:

if (!file.exist()) {
// open the file for writing
...

A similar function definitely exists for accessing files in
UN*X environments.

Thank you, Dan
Jul 17 '05 #1
4 5474
Daniel Polansky wrote:
Is there a way to open file for writing so that the opening
fails if the file already exist? This needs to be atomic
operation so that it is possible to use the file as a
semaphore. As a result, the following coding does not do:

if (!file.exist()) {
// open the file for writing
...

A similar function definitely exists for accessing files in
UN*X environments.

Thank you, Dan

There is the File.createNewF ile() method (JRE >= 1.2). However, the
javadoc does not recommend using it for the purpose you propose but
recommends the FileLock (JRE >= 1.4) class instead.

Ray

Jul 17 '05 #2
I have tried to use FileLock, but it was a bit of pain. Do you know
of a web site containing nice example code of using FileLock? How can
I achieve what I have described in the previous message with FileLock?

By the way, FileLock does not work on Windows 95, but that's not
really a problem, because Windows 95 is not officially supported, or
at least that's what Java installer said to me. My Java program works
on Windows 95 basically fine anyway.

Regards, Dan
There is the File.createNewF ile() method (JRE >= 1.2). However, the
javadoc does not recommend using it for the purpose you propose but
recommends the FileLock (JRE >= 1.4) class instead.

Ray

Jul 17 '05 #3
Daniel Polansky wrote:
I have tried to use FileLock, but it was a bit of pain. Do you know
of a web site containing nice example code of using FileLock? How can
I achieve what I have described in the previous message with FileLock?

By the way, FileLock does not work on Windows 95, but that's not
really a problem, because Windows 95 is not officially supported, or
at least that's what Java installer said to me. My Java program works
on Windows 95 basically fine anyway.

My understanding is that Microsoft no longer supports Windows 95 and
that most other vendors have followed suit.

I may have been overzealous to suggest you couldn't use
File.createNewF ile() for your purpose. The javadoc only says that it is
unsuitable for file locking; perhaps it can work as a semaphore.

In any case, I only just found FileLock myself. Looking at my O'Reilly
"Java NIO" book, there is an example. You may want to buy the book or
the code may be available on their website for free.

Ray

Jul 17 '05 #4
It's not so much that MS doesn't support 95. They don't
support NT either. The sad thing is that these OS still
work but that vendors stopped making programs for them.

phil...
"Raymond DeCampo" <rd******@twcny .rr.com> wrote in message news:p8******** ************@tw ister.nyroc.rr. com...
Daniel Polansky wrote:
I have tried to use FileLock, but it was a bit of pain. Do you know
of a web site containing nice example code of using FileLock? How can
I achieve what I have described in the previous message with FileLock?

By the way, FileLock does not work on Windows 95, but that's not
really a problem, because Windows 95 is not officially supported, or
at least that's what Java installer said to me. My Java program works
on Windows 95 basically fine anyway.

My understanding is that Microsoft no longer supports Windows 95 and
that most other vendors have followed suit.

I may have been overzealous to suggest you couldn't use
File.createNewF ile() for your purpose. The javadoc only says that it is
unsuitable for file locking; perhaps it can work as a semaphore.

In any case, I only just found FileLock myself. Looking at my O'Reilly
"Java NIO" book, there is an example. You may want to buy the book or
the code may be available on their website for free.

Ray

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2946
by: fripper | last post by:
When testing a VB .Net application on my system (with IIS installed) I get an error message when using the fileopen function and the file does not exist ... i.e. I want fileopen to create the file. The message I get is: * * * * * Access to the path "c:\inetpub\wwwroot\_private\(filename)" is denied. The ASP.NET process is not authorized to access the requested resource. For security reasons the default ASP.NET process identity is...
7
12967
by: A_StClaire_ | last post by:
hi, I'm working on a project spanning five .cpp files. each file was used to define a class. the first has my Main and an #include for each of the other files. problem is my third file needs to access the class defined in my second file and I can't figure out how to work this right. if I use an #include in my third file, my Main gives me a compile-time class redefinition error. if I don't, the third file can't "see" the second
4
3656
by: Frank | last post by:
Could someone tell me how to open a file at run time that I didn't know the name of at compile time? I know how to open a file at compile time when I know what the name is going to be. FILE *p_afile; if((p_afile = fopen("shoppinglist.txt", "r")) == NULL) { fprint("Could not open file");
3
34534
by: trellow | last post by:
Hello, I am writing an application that needs to read a file that is already open by another process for writing. When I do the following: FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); I get the following exception:
4
3604
by: Csaba Gabor | last post by:
Up until a few weeks ago, javascript code like window.open("http://mydomain.com", "windowName"); would always bring my new or reused window to the top, with focus. Lately, Firefox (Deer park alpha 2) only brings it to the top if the window is new. If it's being reused, the window does not come to the foreground (with IE 6 it does). Is there a new way in Mozilla/Firefox that I can ensure that this
6
3107
by: Moumen VB.NET 2003/2005 Developer | last post by:
How can I detect if a file sitting on a network drive is still open by another application? This application resides on another machine on the network? I am using VB.NET 2003 Your help is highly appreciated
16
2242
by: Gary Wessle | last post by:
Hi please help with this. std::fstream iofs( f.c_str(), std::ios::in|std::ios::out ); std::cout << f << '\n' << iofs.is_open() << std::endl; puts out **************************************************************** pair_status/myPair
6
34653
by: Ros | last post by:
There are 10 files in the folder. I wish to process all the files one by one. But if the files are open or some processing is going on them then I do not want to disturb that process. In that case I would ignore processing that particular file and move to next file. How can I check whether the file is open or not? I tried os.stat and os.access but I am not getting the expected results. Also I checked in IO exceptions, IO error handler...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7409
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.