Re: RandomAccessFile Seek (find CR)
- From: "Yogi_Bear_79" <nospam@xxxxxxxxxxx>
- Date: Mon, 6 Mar 2006 21:42:56 -0500
"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 thehow is your file encoded? RandomAccessFile is not suited to variable
file,
I want to move the seek point to the begining of that respective line.
So
that the entire line is printed to console.
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));
.
- Follow-Ups:
- Re: RandomAccessFile Seek (find CR)
- From: Knute Johnson
- Re: RandomAccessFile Seek (find CR)
- References:
- RandomAccessFile Seek (find CR)
- From: Yogi_Bear_79
- Re: RandomAccessFile Seek (find CR)
- From: Roedy Green
- Re: RandomAccessFile Seek (find CR)
- From: Yogi_Bear_79
- Re: RandomAccessFile Seek (find CR)
- From: Knute Johnson
- RandomAccessFile Seek (find CR)
- Prev by Date: Re: how to find cpu is idle or not in java
- Next by Date: Windows install locations J2SE and Tomcat
- Previous by thread: Re: RandomAccessFile Seek (find CR)
- Next by thread: Re: RandomAccessFile Seek (find CR)
- Index(es):
Relevant Pages
|