This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

which class to use????  RSS feed

 
raymond yadao
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys!!!
I want to do an application that create a file and save it to my directory.But that file can only be open/modify by my application, no other else.How would i going to do it?

Thanks!
Raymond
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid you will have to write your own operating system to make this possible. On Unix, you could create a file that only the same user (or the sys admin) could read or modify, but not a particular application. On Windows, you can't even do that much with files, but you might be able to use the registry for the same purpose. There are packages that let you access the Windows' Registry through Java, but then your application would be tied to Windows. I think your best bet is to create a read-only file, and put it someplace where clumsy users are unlikely to mess with it.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using JDK 1.4, you can also request a FileLock (obtained from a FileChannel, which you get from a RandomAccessFile, FileInputStream, or FileOutputStream). This will usually (depending on the OS) prevent other programs from accessing your file while your Java program is running and you retain the lock. Once your program exits, the lock will be released, and does nothing to prevent other programs from accessing the file. So this is not a complete solution by any means, but combined with Greg's suggestions it will offer an additional level of security.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!