Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

FileNotFoundException: ..(Access is denied)  RSS feed

 
John Cogan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a class and in one function I open a file to read in from it with the folloing code.
File f = new File(uri);
FileInputStream fis = new FileInputStream(f);
FileChannel fc = fis.getChannel();

// Get a CharBuffer from the source file
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int) fc.size());
Charset cs = Charset.forName("8859_1");
CharsetDecoder cd = cs.newDecoder();
CharBuffer cb = cd.decode(bb);
fis.close();
In another function in the same class I try to print out to the same file with the following code:
FileOutputStream fileOut = new FileOutputStream(uriCurrent);
OutputStreamWriter out = new OutputStreamWriter(fileOut);
out.write(currentTemp.toString());
out.close();
however, I get the error:
java.io.FileNotFoundException: c:\projects\merge\new\master.css (Access is denied)
I know the file exists in the correct location and I closed it with fis.close(); in the first function. I expected it's contents to be overwritten.
Any ideas as to what might be happening would be greatfully received.
Best regards,
John
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting, I wonder if calling close on the channel instead of the input stream would result in the same behavior?
 
John Cogan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jason,
Thanks for replying.
I have tried
fis.close();
fc.close();
both together and separately and together but with no success.
I am unable to delete the file under all conditions. It seems as thought some resource is being kept open but I can't work out what.
Best regards,
John
 
Torsten Schippel
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,
really a strange problem. I just tested it.
The map function of the FileChannel aquires a lock on behalf of the entire JVM.
To work around you can set
bb=null;
or
bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, 0);
Both seams to remove this lock. Additionally you (may) need to force a System.gc();
The doc say something about different OSs. I used JDK 1.4.2. on Win2K.
HTH
Torsten
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the javadoc for FileChannel's map(): "A mapping, once established, is not dependent upon the file channel that was used to create it. Closing the channel, in particular, has no effect upon the validity of the mapping."
I'd say that if you want to be able to open a separate writable stream or channel to the file later on, there doesn't seem to be any reliable way to tell the system that you're done with the MappedByteBuffer and it can forget about it. As Torsten notes, you can null it out, and call system.gc(), but there's no real guarantee that the mapping will be fully cleared by garbage collection.
I'd say your best bet (for something reliable on whatever platform you're on) is to forget about memory mapping here, and just do a read into a plain ByteBuffer:

The while loop usually seems to be unnecessary here, but in theory it could be needed; better safe than sorry.
Of course if your file is bigger than Integer.MAX_VALUE, you'll need more than one ByteBuffer-ful - but I'll let you sort that out if it's an issue.
[ January 09, 2004: Message edited by: Jim Yingst ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!