• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

writing file with RandomAccessFile

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

I work on a windows operating system.
I am trying to create a file on disk at the System.getProperty("user.dir") folder level. The file compiles but no files gets created on file.



Any idea please?

Below the full code sample .



Sincerely,

Maurice
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you get when you print System.getProperty("user.dir")?
 
Bor Kev
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi K.Tsang,

Thanks for taking the time.
when I print System.getProperty("user.dir"), I get : C:\Users\Maurice\Desktop\DVD Project.
when I print System.out.println(System.getProperty("user.dir") + "\\" + DATABASE_NAME)), I get C:\Users\Maurice\Desktop\DVD Project\dvd_db.dvd. Everything compiles , no exceptions at runtime but no file gets created at the path displayed (i.e C:\Users\Maurice\Desktop\DVD Project\dvd_db.dvd). Any ideas how I could persist the file on disk with the RandomAccesfile class?

Sincerely,

Maurice
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The RandomAccessFile class has a bunch of write and writeXXX methods. Using one of these will write output to the intended file.

Currently I don't see any thing that does the writing/output.

Similarly if you want to read data from it, readXXX methods.

Here is an example
 
Bor Kev
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi K. Tsang,

Actually, I found out why I couldn't persist the file on disk : I wasn't using the right constructor. RandomAccessFile provides two constructors and I was using the wrong one :

public RandomAccessFile(File file,String mode) throws FileNotFoundException is the one I should have used to persist a file on disk. It creates a random access file stream to read from, and optionally to write to, the file specified by the File argument


Instead I was using :

RandomAccessFile(String name, String mode) which creates a random access file stream to read from, and optionally to write to, a file with the specified name. This constructor implies that the file -which string name is passed as a parameter- already exists.

Sincerely,

Maurice
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's good to know, so thanks for coming back to post that information!
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic