Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

writing file with RandomAccessFile  RSS feed

 
Bor Kev
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • 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
 
K. Tsang
Bartender
Posts: 3633
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you get when you print System.getProperty("user.dir")?
 
Bor Kev
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • 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: 3633
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 17
1
  • Mark post as helpful
  • send pies
  • 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
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's good to know, so thanks for coming back to post that information!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!