Solved

A simple yet effective way to lock file from reading it

Posted on 2012-04-08
14
260 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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
 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 62
object oriented programming comparison 5 77
null output 3 35
web project error add remove 1 32
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 Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

830 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