473,411 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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 5454
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.createNewFile() 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.createNewFile() 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.createNewFile() 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********************@twister.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.createNewFile() 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
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....
7
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...
4
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...
3
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,...
4
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...
6
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...
16
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...
6
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.