• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

A simple yet effective way to lock file from reading it

Hi,

I would like to know a simple yet effective way to lock a file from reading and writing.

Thanks.
0
wsyy
Asked:
wsyy
  • 5
  • 4
  • 3
  • +1
4 Solutions
 
basicinstinctCommented:
File file = new File("/home/rick/foo");
if(file.exists()){
    file.setReadable(false, false);
}

http://docs.oracle.com/javase/6/docs/api/java/io/File.html#setReadable(boolean, boolean)
0
 
wsyyAuthor Commented:
I wonder if the said solution can prevent anyone from reading the file by editor software or the vi command in Linux.
0
 
wsyyAuthor Commented:
Maybe I would like more out of it.

I want to prevent anyone from reading, modifying, and moving the file either in JVM or at the OS level.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
CEHJCommented:
Not possibly really without native code
0
 
wsyyAuthor Commented:
what does "native code" mean?
0
 
basicinstinctCommented:
setReadable setWriteable and setExecutable do change the underlying file permissions (as long as the java app has the permissions to change the permissions in the first place).

Also the underlying operating system needs to support such file permissions.
0
 
CEHJCommented:
what does "native code" mean?

It means code that's native to the OS - Linux in your case. You don't say what your actual goal is, so it's difficult to say much more
0
 
wsyyAuthor Commented:
My goal is to prevent anyone from touching, meaning reading or modifying or moving, the file my java program is reading from.

The simplest way I can think of is to pretend to write to the file (actually I just read it), such that no one is able to touch it.

But how can I achieve it?


basicinstinct-

I want to change the permission temporarily when my program is reading the file. Also, I want to delete the file after my program finishes reading it.

Will the solution you provided work to the end?
0
 
basicinstinctCommented:
hi wsyy - changing the file permissions is probably not quite what you want because someone will always be able to read it.

i can't help thinking you are trying to solve a problem the wrong way.

perhaps if you give us the problem (WHY do you want to lock the file) rather than the solution (lock the file) we could help you more...
0
 
wsyyAuthor Commented:
The file contains information and format that I don't want to disclose to a client, while I have to deploy the program as well as the file on client's server.

The whole picture is:

The original file is encrypted, which then will be dencrypted in run time to a file which we have been talking about.

The dencrypted file must not be read or copied during it being processed by my program. That is why I plan to lock it from being touched.

After my program finishes, the program will delete the dencrypted file.
0
 
basicinstinctCommented:
Must you decrypt it to the file system?
 What about just decrypting it in memory?
0
 
anilallewarCommented:
You can actually use the file channel methods to lock a file so that it is NOT accessible to any other process of the OS.

FileOutputStream fos= new FileOutputStream("file.txt");
FileLock fl = fos.getChannel().tryLock();

Please see the link below that I had found useful.

http://www.linuxtopia.org/online_books/programming_books/thinking_in_java/TIJ314_030.htm
0
 
CEHJCommented:
FileLock fl = fos.getChannel().tryLock();

Open in new window


could be tried, but the api docs will tell you that the lock is 'advisory', meaning that the lock might not be honoured in all situations

Basicinstinct's suggestion to decrypt it into memory sounds like a good one - why can't you do that?
0
 
anilallewarCommented:
trylock() is a nice way of trying to obtain a lock on a file; but if you absolutely want a lock you can use the
lock()

Open in new window

method instead.

lock( ) blocks until the lock is acquired, or the thread that invoked lock( ) is interrupted, or the channel on which the lock( ) method is called is closed.

I also agree with the idea of decrypting into memory, but it depends on how many files need to be opened concurrently. If its a small number, then we should not have any problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now