BrianMc1958
asked on
How to use truncate with FileChannel?
Dear Experts,
I want to truncate a certain number of bytes from the end of a file. I'm trying this:
FileChannel srcChannel = new FileInputStream(absoluteFi leName).ge tChannel() ;
srcChannel.lock();
srcChannel.truncate(srcCha nnel.size( ) - someNumberOfBytes);
srcChannel.close();
However, I get this error:
java.nio.channels.NonWrita bleChannel Exception
at sun.nio.ch.FileChannelImpl .lock(File ChannelImp l.java:739 )
at java.nio.channels.FileChan nel.lock(F ileChannel .java:865)
at ChopHeaderAndFooter.getLas tLineAndCo unt(ChopHe aderAndFoo ter.java:6 0)
at ChopHeaderAndFooter.doMain (ChopHeade rAndFooter .java:26)
at ChopHeaderAndFooter.main(C hopHeaderA ndFooter.j ava:18)
Any ideas how to make this work? (I'd rather not write the entire file out to another file...)
Thanks!
BrianMc1958
I want to truncate a certain number of bytes from the end of a file. I'm trying this:
FileChannel srcChannel = new FileInputStream(absoluteFi
srcChannel.lock();
srcChannel.truncate(srcCha
srcChannel.close();
However, I get this error:
java.nio.channels.NonWrita
at sun.nio.ch.FileChannelImpl
at java.nio.channels.FileChan
at ChopHeaderAndFooter.getLas
at ChopHeaderAndFooter.doMain
at ChopHeaderAndFooter.main(C
Any ideas how to make this work? (I'd rather not write the entire file out to another file...)
Thanks!
BrianMc1958
Use a FileOutputStream instead of FIS
ASKER
I'm getting:
java.io.FileNotFoundExcept ion: C:\MyFile (Access is denied)
which was the whole point of trying FileChannel in the first place. I've done everything I can to be certain that there on no restrictions of C:\MyFile, but I can't get anything to modify it!
Any more ideas?
Thanks again...
BrianMc1958
java.io.FileNotFoundExcept
which was the whole point of trying FileChannel in the first place. I've done everything I can to be certain that there on no restrictions of C:\MyFile, but I can't get anything to modify it!
Any more ideas?
Thanks again...
BrianMc1958
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It's failing on the first statement below:
FileChannel srcChannel = new FileOutputStream(absoluteF ileName).g etChannel( );
srcChannel.lock();
srcChannel.truncate(srcCha nnel.size( ) - lastLine.length());
srcChannel.close();
I'm as sure as I can be that I have access to the file. However, there might be something I don't understand about the security. As far as I can tell, it's an ordinary file with full access granted to everyone.
FileChannel srcChannel = new FileOutputStream(absoluteF
srcChannel.lock();
srcChannel.truncate(srcCha
srcChannel.close();
I'm as sure as I can be that I have access to the file. However, there might be something I don't understand about the security. As far as I can tell, it's an ordinary file with full access granted to everyone.
van u post a strack trace for the exception
how is absoluteFileName set?
how is absoluteFileName set?
Use a RandomAccessFile to get the channel
> Use a RandomAccessFile to get the channel
that won't make any difference.
that won't make any difference.
i suspect there may be a problem with the file name, try getting rid of the c:
>> Use a RandomAccessFile to get the channel
(Opened in rw mode)
(Opened in rw mode)
> (Opened in rw mode)
Still won't change anything :)
Still won't change anything :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>open in rw mode as per ojects told.
You mean as *i* said ...
You mean as *i* said ...
sorry,use randam acces file ,open in rw mode as per CEHJ told.
I said it won't work, it'll get the same error :)
ASKER
Sorry, folks. Actually took a weekend off.
I'm afraid objects is correct here. I started with randomAccess, using the "rw" option, and I do get the same error. It works with just the "r", but then fails with "rw". That was why I tried switching over to the Channel technique in the first place. The fact that it does read the file OK with the "r" option tells me it's not a question of finding the file.
Thanks again for all your responses. If anyone else has any ideas, I'd be happy to try them. I should say, though, that I've found a klutzy way around the problem for now, using the Channel's transferFrom method to copy everything but the bytes I want into a second file...
--BrianMc1958
I'm afraid objects is correct here. I started with randomAccess, using the "rw" option, and I do get the same error. It works with just the "r", but then fails with "rw". That was why I tried switching over to the Channel technique in the first place. The fact that it does read the file OK with the "r" option tells me it's not a question of finding the file.
Thanks again for all your responses. If anyone else has any ideas, I'd be happy to try them. I should say, though, that I've found a klutzy way around the problem for now, using the Channel's transferFrom method to copy everything but the bytes I want into a second file...
--BrianMc1958
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> I'm afraid objects is correct here.
;)
you need access to the file, without that you cannot change it.
;)
you need access to the file, without that you cannot change it.
>>you need access to the file
Except that wasn't necessarily the error:
>>java.nio.channels.NonWri tableChann elExceptio n
Except that wasn't necessarily the error:
>>java.nio.channels.NonWri
ASKER
Dear Everybody,
I'm afraid I can't pursue this anymore. (I had to put my tail between my legs and take the longer route around the problem.) To anyone reading this, I'm afraid I didn't get an answer this time. But to close the question, I'll split the points.
Thank you all for your help anyway!
--BrianMc1958
I'm afraid I can't pursue this anymore. (I had to put my tail between my legs and take the longer route around the problem.) To anyone reading this, I'm afraid I didn't get an answer this time. But to close the question, I'll split the points.
Thank you all for your help anyway!
--BrianMc1958