Re: RandomAccessFile Seek (find CR)




"Knute Johnson" <nospam@xxxxxxxxxxxxxxxxx> wrote in message
news:Ht2Pf.342489$ek6.212566@xxxxxxxxxxxxxxx
Yogi_Bear_79 wrote:
"Roedy Green" <my_email_is_posted_on_my_website@xxxxxxxxxxxxxx> wrote in
message news:4kln025aq1ompadg20c6tl2bnvg8pg0elb@xxxxxxxxxx
On Sun, 5 Mar 2006 22:16:51 -0500, "Yogi_Bear_79" <nospam@xxxxxxxxxxx>
wrote, quoted or indirectly quoted someone who said :

I am using RandomAccessFile. When I select a random seek point in the
file,
I want to move the seek point to the begining of that respective line.
So
that the entire line is printed to console.
how is your file encoded? RandomAccessFile is not suited to variable
length encodings like UTF-8. You would want to prepare a file of pure
16 bit chars, for example to be able to index without complication.

If you have 8-bit encodings, you must read bytes then convert to char.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.


This is 8-bit. A imple .txt file from notepad. I understadn there
probbaly isn't a real world app for waht I am doing, butthis is just a
stand alone example. How would I read the bytes and convert to charand
test for CR? I have the rest of the code worked out just need a little
help please.

Yogi:

We tried to explain to you that this is the wrong tool for the job. You
could make your data into fixed length records and you wouldn't need to
know anything about the CR or you can use a Reader to read lines of text
out of your text file.

But since you really don't want to know how to do the job correctly I'll
show you how to do it the wrong way.

First note that Notepad doesn't just put a CR at the end of the line it
puts a LF too.

This is pseudo code, you will need to actually write your own code with
all of the Exception handling and missing bits.

open the random access file
seek to starting point
int n = 0;
// while the character isn't a linefeed
while ((n = file.read()) != 0x0a) {
// get the file pointer
long pos = file.getFilePointer();
// seek to byte before last read byte
file.seek(pos-2);
}
// n is now the first character of your line
char[] chars = new char[??];
chars[0] = n;
int i=1;
// while it isn't a carriage return
while ((n = file.read()) != 0x0d)
char[i++] = n;
// when the while above is done your array has i characters in it
String str = new String(chars,0,i);
close the file

This is really MM. I don't know what value it is to learn something that
should never be used in actual work.

--

Knute Johnson
email s/nospam/knute/


Knute,

Thank you so much for indulging me. I fully understand what you were
telling me. This project was for a lab, and was meant for use with a
specific file. The same file is used for a few other labs, so re-creating a
special file was not needed. I have saved your input, for future reference
whenever I have the need to work IRL with RandomAccessFile. I have the
program working, however I have two things I can't pin down. If you don't
mind looking at the code:

1.) Whenever I do a f.readLine() after the final while statement I always
get the word that is on the line below the word "Java". I don't understand
this since the compareTo() method found a match.
2.) About 10% of the time I get a an IO error "negative seek offset" this
always occurs at postion 1. I tried to prevent it in various ways, but I
can't seem to stop it from occuring..

public static void main(String args[]){

long pos = -1;
int n = 0; int i =0;
Random rand = new Random();
String word ="Java";

try{
RandomAccessFile f= new RandomAccessFile("words.txt", "r");

do{
if (pos >= 0 | pos < f.length()){
pos = Math.abs(rand.nextLong()) % f.length();
}
if.seek(pos);
i++;//counter
while ((n = f.read()) != 0x0a){
pos = f.getFilePointer();
f.seek(pos-2);
if(pos > 2){break;};
}
}while((word.compareTo(f.readLine()) != 0));


.



Relevant Pages

  • Re: RandomAccessFile Seek (find CR)
    ... This project was for a lab, and was meant for use with a specific file. ... pos = Math.abs) % f.length; ... When you compare two strings for equality use the equalsmethod of String rather than compareTo. ... else seek the file back two bytes and go back to read a character ...
    (comp.lang.java.help)
  • Re: An Odd Little Script
    ... > like to do is strip out all linefeeds from the file, read the character ... return data, pos ... Terry Hancock ...
    (comp.lang.python)
  • Re: Safest way to convert chars to ints
    ... unsigned integral type - It is most likely implemented ... the variable 'Pos' will equal ... from the next character found. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Reading from very large file
    ... Second, markspace specifically asked for hard numbers and pointed out that adjectives like "extremely big" are not terribly meaningful, yet you ignored that advice and the request and simply provided another vague adjective, "immense", without any indication of what your target performance is. ... You mentioned that "reading each character with a RandomAccessFile is too slow". ... At that point your search for digits is nearly all memory-bound. ...
    (comp.lang.java.programmer)
  • Re: Some suggestion required regarding get line functionality.
    ... buff = malloc; ... position of pos ... but we need to put newline at the end od buf;So ... had read the last character of the file, ...
    (comp.lang.c)