Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

readLine using RandomAccessFile(Urgent)  RSS feed

 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to read whole file line by line using RandomAccessFile.Whole file looks like this:
A:null-null
B:null-null
C:null-null
D:null-null
E:null-null
F:null-null
G:null-null
H:null-null
I:null-null
J:null-null
K:null-null
L:null-null
M:null-null
N:null-null
O:null-null
P:null-null
Q:null-null
R:0-null
S:124-260
T:null-null
U:null-null
V:null-null
X:null-null
Y:null-null
When line contains S, i just store that line in one string. I don't know why instead of S:124-260, i get S : 1 2 4 - 2 6 0. I mean why i get space between characters???
Thanks,
Angela
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angela,
That's odd. I copied your lines into a file named "Input.txt" and read it in and dumped everything to stdout and there were apparently no embedded spaces. Here's my code:

Here's my results:

There must be some other issue here. There is a caveat mentioned in RandomAccessFile.readLine() that may be at work here:

... Each byte is converted into a character by taking the byte's value for the lower eight bits of the character and setting the high eight bits of the character to zero. This method does not, therefore, support the full Unicode character set.

That's just a long shot though.
Michael Morris
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds to me like an encoding problem - the file is probably encoded in UTF-16 or something similar, which uses two bytes per character. RandomAccessFile doesn't handle encodings well, which is one of many reasons why the class should really be avoided. Try using an InputStreamReader to decode the file:
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all,
The problem was when I wrote this file using RandomAccessFile object, I used
raf.writeChars(String a + String b)
When I changed to:
String c = String a + String b;
raf.writeBytes(c);
After that, It's working fine.
Thanks,
Angela
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!