This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URLyBird: about read the records from the database file.

 
Ronggen Liu
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

in Andrew Monkouse's book,there is a inner class RecordFieldReader in class DvdFileAccess. it used to read the record from the database file. and i tried as it, the field value returned includes some irrecognizable characters.

is the encoding not right? how do you encoding the bytes[] to String? it seems the filed value include some number, then it's displayed as irrecognzable character.

Thank you!
Ronggen
 
Anne Crace
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using "US-ASCII" instead of UTF-8. That's what I use, and I don't have any weird chars in my output.
 
Ronggen Liu
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you,Anne Crace,

i tried it, the output also included some irrecognizable chars.can you help to try run the this class to see the output is right or not?


I tried the encoding "US-ASCII","UTF-8" and the system default encoding, all are failed.(the output include the irrecognizable chars for the number 1)

-Ronggen
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Ronggen.

Try this out:



This is actually what you'll have to do in your application. You create an array of bytes (with the size of the field you want to read), then read and store the content of the field in this array of bytes, then transforms it to a String (using new String(array, "US-ASCII")).
 
mohamed sulibi
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi;

also try this :


regards.
Mohamed Darim.
 
jaga
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any difference in the way ? except for the streams used
byte [] b= new byte[4];
FileInputStream fis = new FileInputStream("C:\\jdev\\workspace\\dbdata.db");
DataInputStream dis = new DataInputStream(fis);
dis.read(b, 0, 4);
String S = new String(b,"US-ASCII");
System.out.println(S);
dis.close();
fis.close();


RandomAccessFile ris = new RandomAccessFile("C:\\jdev\\workspace\\dbdata.db", "r");
int magicCookie=ris.readInt();
System.out.println("magicCookie :"+magicCookie);
ris.close();

The results are different :
0000
magiccookie :
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must know, though, that US-ASCII is a 7-bit encoding and my assignment specifies that I ought to use a 8-bit encoding.

Therefore, you might still like to reconsider certain recommendations stated above.
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edwin Dalorzo:
You must know, though, that US-ASCII is a 7-bit encoding and my assignment specifies that I ought to use a 8-bit encoding.

Therefore, you might still like to reconsider certain recommendations stated above.


And for that reason exactly I used: "ISO-8859-1"
The requirement for this is ambiguous and leaves room for design choices.
- must support US-ASCII
- must support 8-bit encoding

Discussion/comparison : utf-8 vs iso-8859-1 vs us-ascii and Joel On Software

Alex
[ April 09, 2008: Message edited by: Alex Belisle Turcot ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic