Link to home
Start Free TrialLog in
Avatar of BrianMc1958
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(absoluteFileName).getChannel();
      srcChannel.lock();
      srcChannel.truncate(srcChannel.size() - someNumberOfBytes);
      srcChannel.close();

However, I get this error:

java.nio.channels.NonWritableChannelException
      at sun.nio.ch.FileChannelImpl.lock(FileChannelImpl.java:739)
      at java.nio.channels.FileChannel.lock(FileChannel.java:865)
      at ChopHeaderAndFooter.getLastLineAndCount(ChopHeaderAndFooter.java:60)
      at ChopHeaderAndFooter.doMain(ChopHeaderAndFooter.java:26)
                at ChopHeaderAndFooter.main(ChopHeaderAndFooter.java:18)

Any ideas how to make this work?  (I'd rather not write the entire file out to another file...)

Thanks!
BrianMc1958

Avatar of Mick Barry
Mick Barry
Flag of Australia image

Use a FileOutputStream instead of FIS
Avatar of BrianMc1958
BrianMc1958

ASKER

I'm getting:

java.io.FileNotFoundException: 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
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It's failing on the first statement below:

      FileChannel srcChannel = new FileOutputStream(absoluteFileName).getChannel();
      srcChannel.lock();
      srcChannel.truncate(srcChannel.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.
van u post a strack trace for the exception
how is absoluteFileName set?
Use a RandomAccessFile to get the channel
> Use a RandomAccessFile to get the channel

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)

Still won't change anything :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>open in rw mode as per ojects told.

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 :)
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> 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

Except that wasn't necessarily the error:

>>java.nio.channels.NonWritableChannelException
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