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

本小妞迷上赌 提交于 2021-02-17 03:56:19

问题


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.


回答1:


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)




回答2:


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){
}



回答3:


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.




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/30749317/writedouble-method-of-dataoutputstream-is-writing-data-in-text-document-in-enc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!