Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Using Java Read .dat file where data is split up into bytes  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a .dat file where the information is split up into bytes 64, 32, 8, 8, 8, 8 respectively.

I know how to read a file that has a know split such as ":" but not a file split based on byte size.

How would i go about doing that?
 
Bartender
Posts: 5319
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are the values arranged in the file? I assume that the file is a text file, ie ASCII characters. How do you tell where one byte leaves off and another begins? Is there some kind of delimiter between the values? Is there one value per line? Can you post a sample of the file?
 
Carey Brown
Bartender
Posts: 5319
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the other hand, if we're talking about a binary file you can open it up with FileInputStream and use one of its read(...) methods.
 
Binnie Ali Abdullahi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so it is a dat file (.dat) there are no delimiters such as ":" or anything else the only thing I know is that each thing is a part is a certain byte size.
first, 64 byte
second 32 byte
third 8 byte
fourth 8 byte
fifth 8 byte
sixth 8 byte

and it keeps looping the file is to be assumed that it has no end
 
Carey Brown
Bartender
Posts: 5319
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
 
Carey Brown
Bartender
Posts: 5319
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you try to open the file with a text editor (e.g. "Notepad"), do you see numbers or gibberish?
 
Binnie Ali Abdullahi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gibberish

Thank you so much for your help and patience by the way
 
Carey Brown
Bartender
Posts: 5319
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, that means you have a binary file. You can use FileInputStream to read it. You can use its read() method to read a single byte at a time, or use read(byte[] ary) to read enough bytes to fill in a byte array of a specific length. The second method returns the number of bytes actually read into the array, which may or may not fill the whole array. WARNING: Technically, you'd have to handle the case where bytes read is less than the bytes in the array in a loop until the array is entirely full. Given that your arrays are so small you'd probably get away with assuming that all the reads completely fill the array, or otherwise it's an error, or End Of File.
 
Sheriff
Posts: 21558
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:WARNING: Technically, you'd have to handle the case where bytes read is less than the bytes in the array in a loop until the array is entirely full. Given that your arrays are so small you'd probably get away with assuming that all the reads completely fill the array, or otherwise it's an error, or End Of File.


To make sure that the entire block is read, you can wrap your InputStream in a DataInputStream and use one of its readFully methods.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!