aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes DataInputStream and Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "DataInputStream and Strings" Watch "DataInputStream and Strings" New topic
Author

DataInputStream and Strings

John Martin
Greenhorn

Joined: Aug 09, 2005
Posts: 8
I am using a DataInputStream to read the database file. However, I am confused at how to read String values as there are no methods listed. The readUTF() (which does return a String) did not function properly.

A reference guide suggests using a ObjectInputStream, but that class has the same methods as DataInputStream.

Also, when reading the 2 byte vlaue for number of fields for each record I received a large value.


scjp / scjd / scwcd / scbcd
Daniel Dalton
Ranch Hand

Joined: Mar 20, 2005
Posts: 146
Take a look at java.io.RandomAccessFile. You can read data into a byte[] and then convert that to a String. It also has various other useful methods that should enable you to read the file fairly readily.
Muthaiah Ramanathan
Ranch Hand

Joined: May 16, 2005
Posts: 102
I am using a DataInputStream to read the database file. However, I am confused at how to read String values as there are no methods listed. The readUTF() (which does return a String) did not function properly.


While many of them prefer RandomAccessFile, I also use DataInputStream to read from the FileInputStream. You can first read the specified number of bytes and then convert it into a String (byte[]->chars->String).

Also, when reading the 2 byte vlaue for number of fields for each record I received a large value.

Try the 'readInt()' method of DataInputStream!


SCJP 1.6, SCJD, SCWCD, SCBCD.

Be nice to people on the way up cos, you'll need 'em on your way down - From somewhere I can't remember!
John Martin
Greenhorn

Joined: Aug 09, 2005
Posts: 8
Hey All - Thanks!

I found some old code Andrew had posted. 'Search' feature came thru on my first question.

Basically just cast each long value you readin for the field name:

//using dataInputStream
byte[] fldName = new byte[fldSz];
long fldNameValue = dis.read(fldName);
String fieldName = (new String(fldName))


However I am still getting too largge a value for the Number of Fields:




Output:

cookie = 538968322
# fields 8199.

I dont think the evaluator will allow me to hard code a 7 for # fields!


Thx for any ideas.
John Martin
Greenhorn

Joined: Aug 09, 2005
Posts: 8
Note: I do see that this has been a recurring question over that past few years.
John Martin
Greenhorn

Joined: Aug 09, 2005
Posts: 8
Note: My output is now 'normal'. I am not sure what the problem was. The original file may have been corrupted? Not sure.

Thanks for the insights!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DataInputStream and Strings