Solved

Get file position

Posted on 2004-08-30
8
237 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
8 Comments
 
LVL 24

Expert Comment

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

Expert Comment

by:girionis
Comment Utility
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
Comment Utility
> constructors (byte array or String with offsets).

should be

write method signatures (byte array or String with offsets).
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 250 total points
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>I need to know the current file pointer position.

Why, may we ask?
0
 

Author Comment

by:kxix
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now