LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2009, 12:57 PM   #1
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Rep: Reputation: 37
Java File reads and writes


Hi everyone,

I would like to ask something about reading and writing to files. First of all I would like to ask about the canRead/canWrite methods in the File. Will these methods return only the access permissions? Does anybody know any method that can be used to determined if the file is read or written by another program at the same time? I have some programs that deal with the same file (reading and writing) at the same time. What I what to do is to avoid conficts between reads and writes.
Would be grateful for any suggestions.

Thanks.
 
Old 04-01-2009, 11:52 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

What you're asking about is "file locking". Java NIO's "FileLock" might be of interest:

http://java.sun.com/developer/JDCTec...02/tt0924.html

'Hope that helps .. pSM
 
Old 04-08-2009, 12:52 PM   #3
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
File Lock Exception

Hi again,

I am using the following code to read an object from a file:

Quote:
FileInputStream fis = new FileInputStream(USERS_DB_PATH);
ObjectInputStream ois = new ObjectInputStream(fis);
FileLock lock = fis.getChannel().tryLock();

if(lock != null)
{
users = (UsersDatabase) ois.readObject();
lock.release();
}
and I get the following exception

Quote:
Exception in thread "main" java.nio.channels.NonWritableChannelException
at sun.nio.ch.FileChannelImpl.lock(FileChannelImpl.java:773)
at java.nio.channels.FileChannel.lock(FileChannel.java:865)
Any ideas why and how can I avoid it?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Use auditing to track reads and writes in a file LXer Syndicated Linux News 0 08-12-2007 10:30 AM
LXer: Use auditing to track reads and writes in a file LXer Syndicated Linux News 0 08-12-2007 01:02 AM
Ratio of Reads/Writes on disk erimar77 Linux - Server 2 08-22-2006 11:16 PM
CD R/RW: reads or writes but not both hatha Linux - General 6 04-17-2005 07:12 PM
cdburner reads not writes NGraphiX Linux - Hardware 1 02-28-2003 04:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration