Writing to and reading from a binary file in java.

Writing to and reading from a binary file in java.

I have written the following code to convert an ASCII text file to a binary file:

public static void main(String args[]){
 DataOutputStream dos = null; Scanner input = null;          
try{
         dos = new DataOutputStream(new FileOutputStream("zipbinary.txt"));
         input = new Scanner(new BufferedReader(new FileReader("zipcode.txt")));
     int zip = 0; double lat = 0; double longt = 0;
    while (input.hasNext()){                     
                      zip = input.nextInt(); 
                  lat = input.nextDouble();
                  longt = input.nextDouble();
                  dos.writeChar(zip);
              dos.writeChar('\t');
              dos.writeDouble(lat);
              dos.writeChar('\t');
              dos.writeDouble(longt);
              dos.writeChar('\n');
     }

I read the binary file from another program as follows:

m_dis = new DataInputStream(new BufferedInputStream(new FileInputStream(m_ZipPath)));
       while (m_dis.available() != 0){
             zipcode = m_dis.readChar();
             String zip = String.valueOf(zipcode);
             m_dis.readChar(); // throws out the tab
             m_dblLat2 = m_dis.readDouble();
             m_dis.readChar(); // throws out the tab
             m_dblLong2 = m_dis.readDouble();
            }
             m_dis.close();

There are 85,000 records in the text file all having the format: 00501 40.815400 -73.045600. The entries I get in the binary file are as follows:

o @Dh -o"F ARBeC Y J3/4j3{ @22^e (system gibberish).

It does work, but the records obtained after reading the binary file are much less than those in the text file. And I get an EOFException at code: mdblLat2 = mdis.readDouble();

I do not know what I am doing wrong. Any help appreciated.

View Answers









Related Tutorials/Questions & Answers:
Writing to and reading from a binary file in java.
Writing to and reading from a binary file in java.  I have written... the binary file from another program as follows: m_dis = new DataInputStream... work, but the records obtained after reading the binary file are much less than
Reading binary file into byte array in Java
Example code of reading binary file into byte array in Java This example shows you how to read a binary file into byte array from Java program. This type... is the complete code of the Java program that reads the binary file into byte array:ADS
Advertisements
reading multiple files from a directory and writing them into a single file
reading multiple files from a directory and writing them into a single file... file i get an empty text file can you guide me how to do this: /*this program reads multiple files * from a single directory. */ package elite.tech.com
Writing and Reading A File
Writing and Reading A File  Hello, I've been trying to learn writing and reading data from file for our assignment, but just stuck on how to proceed...;To extract the data from text file and save it to jtable, visit the following links
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.  Hello, I would like to know how to write... stumped when it comes to reading the file and writing to a new file. My code
Reading a file from Jar JAVA
Reading a file from Jar JAVA  I have added one excel sheet into a jar file. can anybody tell me how i can read that file. actually when i am running code from eclipse i able to read it but when i am adding that jar file
Reading a file from Jar JAVA
Reading a file from Jar JAVA  I have added one excel sheet into a jar file. can anybody tell me how i can read that file. actually when i am running code from eclipse i able to read it but when i am adding that jar file
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.  Hello, I would like to know how to write... it comes to reading the file and writing to a new file. My code is as follows
Reading text from image file - Java Beginners
Reading text from image file  How Read text from image file
initialise array by reading from file - Java Beginners
initialise array by reading from file  Hello, I wnat to know how i would initialise an array by reading a text file, which contains a simple pattern. for example the file may look as shown below, with the star character
Reading and Writing files - Java Beginners
Reading and Writing files  Hello, please help me to Develop a simple Java application that, when run, Welcomes the users and tells them the name... from a file. After welcoming the users, ask them for their name and save
Reading a text file in java
in java.io.* package for reading and writing to a file in Java. To learn more about reading text file in Java see the tutorial Read File in Java. Thanks...Reading a text file in java  What is the code for Reading a text file
Writing and reading from/to a serialized file through Hash Table in Java
Writing and reading from/to a serialized file through Hash Table in Java... illustrates you how to read and write from/to a serialized file through the hash table in Java. This section provides an example with the complete code of the program
Java read binary file
at Reading binary file into byte array in Java. Thanks   Hi, There is many more examples at Java File - Learn how to handle files in Java with Examples...Java read binary file  I want Java read binary file example code
Reading string from file timed
Reading string from file timed  So I want to make a file reader/ buffered reader that reads a new line of the textfile, lets say every 30 second. Like it reads the first line, waiting 30 seconds, read the next line and so one
reading dropdown values from properties file in jsp
reading dropdown values from properties file in jsp  reading dropdown values from properties file in jsp
Java error reading from file
Java error reading from file       Java error reading file... reading from file. For this we have a class error reading from file. Inside
Reading big file in Java
Reading big file in Java  How to read a big text file in Java program?   Hi, Read the complete tutorial at How to read big file line by line in java? Thanks
Reading the data from property file from java stand alone application in linux
Reading the data from property file from java stand alone application in linux  Please send me the example of reading the data from a property file from java stand alone application in linux. I don't know how to give path
reading a csv file from a particular row
reading a csv file from a particular row  how to read a csv file from a particular row and storing data to sql server by using jsp servlet
java binary file io example
java binary file io example  java binary file io example
binary search tree from text file
binary search tree from text file  How so I go about constructing a binary search tree from a text file, which has letters and numbers, which must be sorted and printed in ascending order. E.g. Text file contents 3 apples pears
Best way to reading file in java
Best way to reading file in java  Hi, As a beginner I want to learn about Java programming and make a program for reading a text file in java. What is the best way for reading file in Java? Thanks
jar file not reading input from serial port
jar file not reading input from serial port  i used a coding for getting data from serial port. JOptionbox with "port not found" message is shown when i execute thru jar file. but when i execute thru netbeans, data is received
jar file not reading input from serial port
jar file not reading input from serial port  i used a coding for getting data from serial port. JOptionbox with "port not found" message is shown when i execute thru jar file. but when i execute thru netbeans, data is received
reading data from excel file and plotting graph
reading data from excel file and plotting graph  I am doing a project using NetBeans in which i have to take input an excel file and then using... at the same time from one excel file using different columns..how can i do that? waiting
Reading duplicate keys from .properties file - Development process
Reading duplicate keys from .properties file  Hi, I am reading a .properties file using a basic java program.Though the .properties file will not allow duplicate keys, but still I want to develope a program to identify
File Writing - Java Beginners
File Writing  Hi... I need a syntax or logic or program by which we can write into the desired shell of the Excel file from console window...  Hi friend, I am sending you a link. This is will help you. Please
xml file reading using java
xml file reading using java  hi deepak I want to read some data from xml file and send that output to particular email address using java   import org.w3c.dom.*; import org.w3c.dom.Node; import javax.xml.parsers.
Reading And Writing Excel File
reading and writing excel file   ... words, we can say that it is used to read the excel file. After reading the excel... the excel sheet  using java . The package we need to import is : java.io.
writing document (.doc) file from java with format such as font type, font style
writing document (.doc) file from java with format such as font type, font style  Can anyone help me how can i write a java code to write a string with format such as font type and style to a document file? Thanks in advance
How to write in Binary file in Java programming language
How to write in Binary file in Java programming language  I need some help about Java programming. Please explain me "How to write in Binary file in Java programming language
Video Tutorial of reading file in Java
is read-only in Java Reading binary file into byte array... get the source code of reading a text file in Java. This source code can be run in Eclipse or from the command prompt also. Reading a file line
Java Write To File Binary
Java Write To File Binary In this tutorial you will learn how to write to binary file. A binary file is a file into which the bit patterns of mostly data types can be formed to a byte of 8 bits. Write to a binary file in java
Java read file
when the file in huge. It is wrapped around FileReader. Java supports reading from a Binary file using InputStream. Close() method must be called every time...There are many ways to read a file in Java. DataInputStream class is used
Writing for the Purpose of Reading
Writing for the Purpose of Reading   ... of such an operation can vary from browser to browser. - The texts should be organized... disoriented. - The advantage of web writing is that a greater number of lists
reading and displaying svg file through java
reading and displaying svg file through java  sample program for reading a svg file
Writing xml file - Java Beginners
Writing xml file  Thank you for the quick response The values which... values from the database. If it is still confusing i'l post you my question again... XmlServlet().createXmlTree(doc); System.out.println("Xml File Created
Reading RDF file using Java code in Eclipse
Reading RDF file using Java code in Eclipse  Could you please tel me what this statement means - Model model = ModelFactory.createDefaultModel
Java File Binary
Java File Binary In this section, you will learn how to write numeric data into the binary file. Description of code: Numeric data converts compactly and faster in a binary format than the text. In the given example, at first, we have
Java Binary data file - Java Beginners
Java Binary data file  Hi, I have a binary data file(binfile.data) and the file has what is commonly referred to as variable length records.Specifically the format of the file is: HEADER SECTION//RECORD
reading the records from a .xlsx file and storing those records in database table
reading the records from a .xlsx file and storing those records in database table  Here is my requirement, I want to read the records from a .xlsx file and store that records in database table. I tried like this public class
reading from pdf to java - Java Beginners
reading from pdf to java  How can i read the pdf file to strings in java. I need the methods of reading data from file and to place that data in the string, using java.   Hi Friend, We are providing you a code
Java example for Reading file into byte array
Java example for Reading file into byte array. You can then process the byte... in Java for reading file into byte array. Sometimes it becomes necessary... of reading file into byte array: Java file to byte array
Reading XML from a File
Reading XML from a File       This Example shows you how to Load Properties from the XML file via... to create new DOM parsers. Some of the methods used for reading XML from
accessing data from a json file writing javascript or using jQuery
accessing data from a json file writing javascript or using jQuery  I am trying to create a page from json objects using javascript. I have... page is built from this json file, but I can't figure out how to code
How to read file in java
How to read file in java Java provides IO package to perform reading... file line by line  in java. FileInputStream- This class reads bytes from... to read file in Java?" Another way of reading a file: The another
svg file reading and display
svg file reading and display  i want a sample program for reading and displaying the content of a svg file
java program for writing xml file - Java Beginners
java program for writing xml file  Good morning i want to write values from my database(one table)into one xml file. Like i have 3 coloumns in my... xml file and storet that in particlar location. Please help me out Thanks
Thread for reading txt file
Thread for reading txt file  how to use 3 thread to read 3 txt file? To create three threads for reading the file and three threads for getting the strings out of the queue and printing them. thanks

Ads