Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Writing to and reading from a binary file in java.
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Jimmy
Writing to and reading from a binary file in java.
0 Answer(s)      10 months ago
Posted in : Java Beginners


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.


Advertisement
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... of the Java program that reads the binary file into byte array: import java.io.
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 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 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
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 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 binary file io example
java binary file io example  java binary file io example
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
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 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
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
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
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
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
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
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.
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
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
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
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
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
 

 

 

DMCA.com