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
Ads
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
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
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
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
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 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
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
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 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
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
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
Video Tutorial of reading file in Java
Reading binary file into byte array in
Java
Next: Check the
Java... 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 by is
perfect way
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