How to read integers and date in binary file


I have a binary file of records. Each record is of the following format: Each record consists of two integers, number of vehicles and number of persons involved; a floating point number, severity code; and a string which contains the date of the accident. This date takes the following form: a three letter month abbreviation followed by a space, then a day of the month followed by a comma, and finally the year which may be represented by a two digit or four digit number.

Following is the code.But I am not getting the proper answer..

my code:

import java.io.*; public class BoydBAssignment5_Ver1 { public static void main(String s[]) { DataInputStream input1; //you need these two variable for a file File infile1; input1 = null; BoydBAssignment5_Ver1 tfr; //this is your program object tfr = new BoydBAssignment5_Ver1(); try{ //try for open infile1 = new File("assign5.data"); input1 = new DataInputStream(new FileInputStream(infile1)); } catch (IOException i){ i.printStackTrace();} tfr.read_records(input1); try { //try for close input1.close(); } catch (IOException i) { System.out.println("error in close"); } } private void read_records(DataInputStream is2) { int totalVehicles=0,totalPersons=0; int numVehicles; int numPeople; char ch; try { //try for read while(true) { numVehicles=is2.readInt(); if(numVehicles==0) break; totalVehicles+=numVehicles; System.out.print("\n"+numVehicles+"\t"); numPeople=is2.readInt(); if(numPeople==0) break; totalPersons+=numPeople; System.out.print(numPeople+"\t"); System.out.print(is2.readDouble()+"\t"); /*System.out.print(is2.readLine()+"\n"); for(int k=0;k<4;k++) { is2.readByte(); }*/ while((ch=(char)is2.readByte()) != 0x00) { System.out.print(ch+""); } } System.out.println("\nTotal no of vehicles:"+totalVehicles); System.out.println("Total no of Persons:"+totalPersons); write_in_file(totalVehicles,totalPersons); } catch (IOException i) { System.out.println("error in write"); } } private void write_in_file(int totalVehicles, int totalPersons) { try { FileWriter fstream = new FileWriter("finalOutput.data"); BufferedWriter out = new BufferedWriter(fstream); out.write("Number of vehicles involved"+"\t"+totalVehicles); out.write("\nNumber of persons involved"+"\t"+totalPersons); out.close(); } catch(Exception e) { e.printStackTrace(); } } }

Output ::


148308 1 4.774904982E-314<br /> 189429102 540161068 1.4275957977117199E-71<br /> 512 576 6.903600071305329E-93 8,98 37966848 512 1.7668797952966E-311<br /> Mar 4,2006 37966848 256 2.8513257442947E-311<br /> Apr 8,2011 38683904 512 2.3101107177838E-311 Jun 14,06 38683904 256 6.792508527386E-312 Apr 22,2005 38683904 256 1.2216329768334E-311 Oct 1,04 38683904 768 2.3099515681247E-311 Jul 9,83 38813952 256 6.802588006634E-312 Aug 4. 1998 38813952 512 6.802588006634E-312 Jun 14, 2011 38813952 512 1.7667206456376E-311 Sep 8, 99 39344128<br /> Total no of vehicles:576033218 Total no of Persons:540165485


Please help me..Thanks in advance!!


By guess is that you data is store as little-endian. DataInputStream is big-endian. (If you are not sure what endianness is see <a href="http://en.wikipedia.org/wiki/Endianness" rel="nofollow">http://en.wikipedia.org/wiki/Endianness</a>)

The simplest thing to do is to read the file into a single direct ByteBuffer which allows you to set the byte order.


  • Trying to make my android application put files on my ftp server
  • Jquery datatables - selected row hightlight is not working in second page
  • How to read data from a text file and save some data from it to an Array
  • How to decode the H.264 video stream received from parcelfiledescriptor
  • rxkotlin groupby is not working
  • Jquery - Differentiate between 'click' and 'focus' on same input when using both
  • Can I make JSF2 skip rendering my custom tag without modifying the tag itself?
  • Converting a PEM private key file to a JAVA PrivateKey Object
  • Go lang multiplexing all goroutines are asleep - deadlock
  • Batch file working differently if ran in administrator mode
  • Can I display google adwords (AdView) in javafx on android
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Align navbar back button on right side
  • How to add date and time under each post in guestbook in google app engine
  • How to set/get protobuf's extension field in Go?
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • Weird JavaScript statement, what does it mean?
  • How do you troubleshoot character encoding problems?
  • Web-crawler for facebook in python
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • How to format a variable of double type
  • Cannot Parse HTML Data Using Android / JSOUP
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • Java static initializers and reflection
  • LevelDB C iterator
  • apache spark aggregate function using min value
  • unknown Exception android
  • Sorting a 2D array using the second column C++
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?