• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

URLyBird: about read the records from the database file.

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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")).
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi;

also try this :


regards.
Mohamed Darim.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic