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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Polansky

    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
  • Raymond DeCampo

    #2
    Re: To open file for writing only if it does not already exist

    Daniel Polansky wrote:[color=blue]
    > 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[/color]


    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

    Comment

    • Daniel Polansky

      #3
      Re: To open file for writing only if it does not already exist

      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
      [color=blue]
      > 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[/color]

      Comment

      • Raymond DeCampo

        #4
        Re: To open file for writing only if it does not already exist

        Daniel Polansky wrote:[color=blue]
        > 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.
        >[/color]
        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

        Comment

        • Phil

          #5
          Re: To open file for writing only if it does not already exist

          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" <rdecampo@twcny .rr.com> wrote in message news:p8A0b.1173 41$wk4.83012@tw ister.nyroc.rr. com...[color=blue]
          > Daniel Polansky wrote:[color=green]
          > > 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.
          > > [/color]
          > 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
          >[/color]

          Comment

          Working...