• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

File I/O

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can we replace the following piece of code with the one listed below it?



Replace with
 
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why not ?

but it seems unnecessary, FileWriter does create a new file before writing to it (windows).. and if any file exists the FileWriter overwrites it. but file.createFile() doesn't create a new file if the file already exists.


Note: from javadocs

Whether or not a file is available or may be created depends upon the
* underlying platform. Some platforms, in particular, allow a file to be
* opened for writing by only one FileWriter (or other file-writing
* object) at a time. In such situations the constructors in this class
* will fail if the file involved is already open.
 
Puja Sinha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Robin,

Thanks for your response.
But as per the javadoc you have attached :

Robin John wrote: Some platforms, in particular, allow a file to be opened for writing by only one FileWriter (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.



Does it mean that if there are two threads trying to work on the same file and one thread already has the file opened up then the other thread will fail to get an instance of the writer object ?

but if one thread has written to and closed the file, then the other thread will overwrite that file and open it ?
 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puja Sinha wrote:Hi Robin,

Thanks for your response.
But as per the javadoc you have attached :

Robin John wrote: Some platforms, in particular, allow a file to be opened for writing by only one FileWriter (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.



Does it mean that if there are two threads trying to work on the same file and one thread already has the file opened up then the other thread will fail to get an instance of the writer object ?




... will fail to write to the file... and why dont you try ?

Note: you should not use it as a file lock mechanism.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic