Solved

A simple yet effective way to lock file from reading it

Posted on 2012-04-08
14
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

628 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