How do I get the byte position of the first character in a line?? the first chracter byte position of each reader.readline???

How do I get the byte position of the first character in a line??  the first chracter byte position of each reader.readline???


  private static void displayTextInputStream(InputStream input)
    throws IOException {
          // Read one text line at a time and display.
        BufferedReader reader = new BufferedReader(new
                    InputStreamReader(input));
        while (true) {
            String line = reader.readLine();
            if (line == null) break;

            System.out.println("    " + line);
        }
        System.out.println();
    }
cbruneAsked:
Who is Participating?
 
krakatoaCommented:
Depends if your 'records' are all the same length. If they are then using a RandomAccessFile you can seek() to the correct starting offset. Otherwise your choices are to read in and count the lines, and stop when you reach the line you are looking for.

But your goal contains contradictions. You say that you want to "reading line by line", but then say you want to skip to that position. Well, once you've read in the lines, you will already be at that point, so you won't need to skip anywhere. Or . . . ?
0
 
krakatoaCommented:
String line = reader.readLine();

Each 'line' is a String. So the position of the first character in it will be 0 in the CharSequence array.
0
 
cbruneAuthor Commented:
I mean the overall byte mark of the starting character for the whole file
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
krakatoaCommented:
I mean the overall byte mark of the starting character for the whole file

What do you mean by "overall byte mark" ? The number of bytes will usually be the same as the number of characters in the file.
0
 
cbruneAuthor Commented:
if i need to skip to the beginning of a certain line in  the file.  as you are reading line by line I need the first byte position marker of where it is in the whole file so I can skip to that particular position.


"position here"test,test,test
"position here"test1,test1,test1
"position here"test2,test2,test2

So I can skip to the front of the line and read the whole record
0
 
awking00Commented:
What defines a "certain line"? How would the byte count be used to find it? There may be another method to accomplish what you need. If you could provide a little more description, perhaps with examples, of your requirement, it may help us with a solution.
0
 
krakatoaCommented:
Closed.
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.

All Courses

From novice to tech pro — start learning today.