Solved

A simple yet effective way to lock file from reading it

Posted on 2012-04-08
14
257 Views
Last Modified: 2012-04-24
Hi,

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

Thanks.
0
Comment
Question by:wsyy
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 23

Accepted Solution

by:
basicinstinct earned 59 total points
ID: 37820786
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
 

Author Comment

by:wsyy
ID: 37820789
I wonder if the said solution can prevent anyone from reading the file by editor software or the vi command in Linux.
0
 

Author Comment

by:wsyy
ID: 37820791
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 37821018
Not possibly really without native code
0
 

Author Comment

by:wsyy
ID: 37821039
what does "native code" mean?
0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 37821068
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 37821336
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:wsyy
ID: 37823684
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
 
LVL 23

Expert Comment

by:basicinstinct
ID: 37825302
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
 

Author Comment

by:wsyy
ID: 37826045
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
 
LVL 23

Expert Comment

by:basicinstinct
ID: 37826206
Must you decrypt it to the file system?
 What about just decrypting it in memory?
0
 
LVL 11

Assisted Solution

by:anilallewar
anilallewar earned 117 total points
ID: 37826940
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 59 total points
ID: 37829247
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
 
LVL 11

Assisted Solution

by:anilallewar
anilallewar earned 117 total points
ID: 37831117
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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now