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

BrianMc1958Asked:
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:

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

--BrianMc1958
CEHJCommented:
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.
CEHJCommented:
>>you need access to the file

Except that wasn't necessarily the error:

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

--BrianMc1958
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
Java

From novice to tech pro — start learning today.