Solved

Get file position

Posted on 2004-08-30
8
238 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 68
Why does the Android Studio logcat show this error when the user presses the button? 2 42
varialbe initialization 11 30
MySQL  on Tomcat 8 30
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

863 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

27 Experts available now in Live!

Get 1:1 Help Now