writeDouble() method of DataOutputStream is writing data in text document in encoded form

2.2k views Asked by At

I have the following code

public static void main(String aed[]){
    double d=17.3;
    try{
            DataOutputStream out=null;
            out=new DataOutputStream(new BufferedOutputStream(new FileOutputStream("new.txt")));
            out.writeDouble(d);
            out.flush();
        }catch(FileNotFoundException fnf){
            fnf.printStackTrace();
        }catch(IOException io){
            io.printStackTrace();
        }
}

Now I am writing this double value to a text file new.txt , but following value is getting in text file

@1LÌÌÌÌÍ

But when i use

out.writeUTF(""+d)

It works fine. Please explain the encoding that is going on here.

5

There are 5 answers

0
inigoD On BEST ANSWER

With DataOutputStream you are writing bytes, the bytes that represent a double value (which is a number value) and not the readable version of that number.

Example:

int i = 8;

In binary i value is '0100' and that's the value that the computer manages.... But you don't want to write the bits '0100' because you want something to read, not it's value; you want the CHARACTER '8', so you must transform the double to character (to String is also valid because is readable)....

And that's what you are doing with ("" + d): transforming it to String.

Use Writer to write text files (BufferedWriter and FileWriter are available, check this for more details)

0
Mohammed Sohail Ebrahim On

In java there are generally two classes of variables namely reference and primitive types.

Your primitive types include int,double,byte,char,boolean,long,short and float. These store one value and are represented in memory by a unicode 16 bit integer.

Reference types hold storage locations and referneces to certain objects. ( string/UTF is a refernce type) hence the actual value is seen

A binary file is not meant to be read by you but by a program that will fetch the values in the correct form and order and the methods you are using should be used solely for writing to a binary file(.dat) which holds actual data values in their respective forms (int,double etc). When writing to a textfile (.txt) text should be written only hence strings.

Writing to a Textfile :

try{
PrintWriter write=new PrintWriter("your filepath",true);
write.println("whatever needs to be written");

write.close();
}
catch(FileNotFoundException){
}

Reading :

Scanner read;
try{
read=new Scanner(new FileReader("your path"));


while(read.hasNext()){
System.out.println(read.nextLine);
}

read.close();
}

catch(FileNotFoundException e){
}
0
Anil Reddy Yarragonda On

The java.io.DataOuputStream.writeUTF(String str) Writes two bytes of length information to the output stream, followed by the modified UTF-8 representation of every character in the string s.

writeDouble(double v)

Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first.

Read the Javadoc:

https://docs.oracle.com/javase/7/docs/api/java/io/DataOutputStream.html

0
Ouney On
writeDouble(Double)

method does not use UTF-8 encoding. If you have written a double using writeDoble() then you should read it using readDouble method of DataInputStream. These files are not meant to be modified or read manually. If you want to put it in plain then stick to writeUTF method.

From Documentation -

writeDouble  - 

Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first.

0
cybersoft On

writeDouble (as another writeByte, writeShort, etc. with corresponding size of bytes) writes 8 bytes of double value representation. That's why class called as DataOutputStream (Data).

writeUTF writes 2 bytes of length and actual string.