• 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

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

 
Greenhorn
Posts: 22
Opera Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
Opera Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic