DataOutputStream的writeDouble()方法以编码形式在文本文档中写入数据 [英] writeDouble() method of DataOutputStream is writing data in text document in encoded form

查看:57
本文介绍了DataOutputStream的writeDouble()方法以编码形式在文本文档中写入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

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();
        }
}

现在我正在将此double值写入文本文件new.txt,但是以下值正在文本文件中获取

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

@1LÌÌÌÌÍ

但是当我使用

out.writeUTF(""+d)

工作正常.请解释此处的编码.

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

推荐答案

使用 DataOutputStream 写入字节,字节表示双精度值(即数字值),而不是可读版本那个数字.

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.

示例:

int i = 8;

int i = 8;

在二进制中,i的值为'0100',这是计算机管理的值....但是,您不想写入位'0100',因为您想要读取某些内容,而不是它的值.您需要CHARACTER'8',因此必须将double转换为character(因为可读性,转换为String也是有效的)....

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)....

这就是您要使用(" + d):将其转换为String.

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

使用Writer编写文本文件(可使用BufferedWriter和FileWriter,请检查有关更多详细信息)

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

这篇关于DataOutputStream的writeDouble()方法以编码形式在文本文档中写入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆