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.truncate(srcChannel.size() - someNumberOfBytes);

However, I get this error:

      at java.nio.channels.FileChannel.lock(
      at ChopHeaderAndFooter.getLastLineAndCount(
      at ChopHeaderAndFooter.doMain(
                at ChopHeaderAndFooter.main(

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


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
Use a FileOutputStream instead of FIS
BrianMc1958Author Commented:
I'm getting: 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...
Mick BarryJava DeveloperCommented:
which method is throwing that exception?
Are you sure you have access to that file

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

BrianMc1958Author Commented:
It's failing on the first statement below:

      FileChannel srcChannel = new FileOutputStream(absoluteFileName).getChannel();
      srcChannel.truncate(srcChannel.size() - lastLine.length());

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.
Mick BarryJava DeveloperCommented:
van u post a strack trace for the exception
how is absoluteFileName set?
Use a RandomAccessFile to get the channel
Mick BarryJava DeveloperCommented:
> Use a RandomAccessFile to get the channel

that won't make any difference.
Mick BarryJava DeveloperCommented:
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)
Mick BarryJava DeveloperCommented:
> (Opened in rw mode)

Still won't change anything :)
Manikandan ThiagarajanSenior consultantCommented:
use randam acces file ,open in rw mode as per ojects told.

calculate the total length of bytes.

uss seek method to find the position in files.
>>open in rw mode as per ojects told.

You mean as *i* said ...
Manikandan ThiagarajanSenior consultantCommented:
sorry,use randam acces file ,open in rw mode as per CEHJ told.

Mick BarryJava DeveloperCommented:
I said it won't work, it'll get the same error :)
BrianMc1958Author Commented:
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...

I meant use RAF *and* Channel
Mick BarryJava DeveloperCommented:
> 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:

BrianMc1958Author Commented:
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!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.