Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

Get file position

Hello,

I'm writing a text file. For reasons of convienience I use the PrintWriter object.

My problem is that at certain points during the file write operation I need to know
the current file pointer position. If this was a RandomAccessFile I would simply
do getFilePointer() method.

How do I know which byte position I'm at ?   Is there another way of doing this ?

Thanks.
0
kxix
Asked:
kxix
1 Solution
 
sciuriwareCommented:
You can by using a "channel".
;JOOP!
0
 
girionisCommented:
How are you writing the data? Character by character? If yes then you coudl use a counter and count how many characters you write and then save the position somewhere. If you write a String then count the length of the string and get the last position every time. Do likewise for the other constructors (byte array or String with offsets).
0
 
girionisCommented:
> constructors (byte array or String with offsets).

should be

write method signatures (byte array or String with offsets).
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
sciuriwareCommented:
Example I used for input monitoring:

import java.nio.channels.FileChannel;

         FileInputStream fi = new FileInputStream(source);
         FileChannel fic = fi.getChannel();
.....
               position = fic.position();

And so on.

;JOOP!
0
 
CEHJCommented:
>>I need to know the current file pointer position.

Why, may we ask?
0
 
kxixAuthor Commented:
The simplest answer seems to be to use a Channel as suggested by "sciuriware".
I therefore accept this answer.
0
 
sudhakar_koundinyaCommented:
>> The simplest answer seems to be to use a Channel as suggested by "sciuriware".

Then we you gave just 'C' to he or she ???
0
 
sciuriwareCommented:
Correct English would be:

"Then, why did you just give 'C' to him or her?"

And his opinion is .......  I don't care too much.

;JOOP!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now