how to write a double to a file in java

G

Gaurav

Hello,

I am trying to figure out how to write double values to a file in
java.

I tried using RandomAccessFile writeDouble but when i look in the file
i see all junk...

what is the correct way of writing doubles to a file so i can read
them .

thank you,
Gaurav
 
C

Chris Smith

Gaurav said:
I am trying to figure out how to write double values to a file in
java.

I tried using RandomAccessFile writeDouble but when i look in the file
i see all junk...

what is the correct way of writing doubles to a file so i can read
them .

You need to be more precise about what you want. Do you want doubles in
text format? If so, convert your doubles to String first --
String.valueOf(myDouble) -- and then write them to a text stream in a
known text encoding (such as ASCII or UTF-8) with Writer.write.
Something like:

FileOutputStream stream = new FileOutputStream("myfile.txt");
OutputStreamWriter out = new OutputStreamWriter(stream, "US-ASCII");
out.write(String.valueOf(myDouble));

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
V

VisionSet

Gaurav said:
Hello,

I am trying to figure out how to write double values to a file in
java.

I tried using RandomAccessFile writeDouble but when i look in the file
i see all junk...

what is the correct way of writing doubles to a file so i can read
them .

Works okay for me:

try{

RandomAccessFile raf = new RandomAccessFile("RafTest","rw");

raf.writeDouble(12.34);

raf.close();

raf = new RandomAccessFile("RafTest","r");

double dub = raf.readDouble();

System.out.println(dub);
}

catch(Exception e) {
e.printStackTrace();
}
 
Joined
Jan 9, 2008
Messages
1
Reaction score
0
writing double values to text file

Hii everybody...
I want to create an output file and then write a point of double values into a text file using java.
eg: (2.00,1.11)

Is there a way to do that. Please let me know.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top