aspose file tools*
The moose likes I/O and Streams and the fly likes Inputting both fixed-length strings and 32-bit integers from the same file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Inputting both fixed-length strings and 32-bit integers from the same file" Watch "Inputting both fixed-length strings and 32-bit integers from the same file" New topic
Author

Inputting both fixed-length strings and 32-bit integers from the same file

Thomas Kiersted
Greenhorn

Joined: Feb 24, 2007
Posts: 22

I usually use Scanner for inputting from files, but now I'm trying to input a from a non-Java optimized file that contains both fixed-length strings (effectively char arrays, of varying lengths), and 32-bit integers. There is a specified format for this, but the integers and strings are interspersed. For example, this might be an input file:



Supposing that book, meow, and boom were four-letter char arrays and strawberry was a ten-letter char array. I know what the format is ahead of time, so I can program it so it anticipates when a 4-char array/10-char array/32-bit integer/etc. is coming. The problem I've run into is, I don't know of a Java input method that will handle both fixed-length strings and integers. It seems like there ought to be one, but if there is, I haven't encountered it.

Scanner is my default, but I don't see any way to input fixed-length strings with it. As there's no Java-specific formatting in my input files, next() and nextLine() aren't adequate.

BufferedReader does what I need for the fixed-length strings, but I just get  (squares) when I try to input integers with it. Adding a StreamTokenizer to it doesn't help at all with the integers, and tokenizes the fixed-length strings incorrectly.

FileInputStream didn't seem to be doing anything correctly, and didn't work well at all for the fixed-length strings, giving me such nonsense as [B@45a877.

So far the best I've got is:



It doesn't work if I try making thirtyTwoBitInt a Byte or an int, either - BufferedReader can only read in fixed-length char arrays, not fixed-length integers.

So what is the class that I'm looking for that will let me input both fixed-length strings and 32-bit Integers, as I specify? I figure there has to be some such class out there, but I'm not finding it.

edit: Tried DataInputStream, too, but its method to input characters is two bytes at a time, and the characters in my file are one byte each.
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

DataInputStream is probably what you want. However, you will need to read each string as a byte array initially, then create a Java string from the bytes with the correct encoding (US-ASCII, ISO-8859-1, etc.). There is a constructor on String to do this, either using a character set name, or a Charset object.
Thomas Kiersted
Greenhorn

Joined: Feb 24, 2007
Posts: 22

Thanks! I found the String constructor you're referring to in the Javadocs, and it seems to be doing the trick, along with ISO 8859-1. Only other issue I've run into is Big Endian/Little Endian integers (my file uses Little Endian, Java defaults to Big Endian), and Integer.reverseBytes(int) appears to have solved that issue.

Great job knowing just what I needed!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inputting both fixed-length strings and 32-bit integers from the same file