Solved

Get file position

Posted on 2004-08-30
8
245 Views
Last Modified: 2010-05-18
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
Comment
Question by:kxix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 11930044
You can by using a "channel".
;JOOP!
0
 
LVL 35

Expert Comment

by:girionis
ID: 11930061
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
 
LVL 35

Expert Comment

by:girionis
ID: 11930066
> constructors (byte array or String with offsets).

should be

write method signatures (byte array or String with offsets).
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 24

Accepted Solution

by:
sciuriware earned 250 total points
ID: 11930074
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11930142
>>I need to know the current file pointer position.

Why, may we ask?
0
 

Author Comment

by:kxix
ID: 11949152
The simplest answer seems to be to use a Channel as suggested by "sciuriware".
I therefore accept this answer.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11949161
>> 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
 
LVL 24

Expert Comment

by:sciuriware
ID: 11949269
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question