Java 将 double 写入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9756939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:15:23  来源:igfitidea点击:

write double into a file

javafilechardoubletypeconverter

提问by lonesome

How can I convert a double into a const char, and then convert it back into a double? I want to convert the double to a string, to write it to a file and then when I read the file, it needed to be converted back into a double

如何将双精度转换为常量字符,然后将其转换回双精度?我想将双精度转换为字符串,将其写入文件,然后当我读取文件时,需要将其转换回双精度

采纳答案by John3136

Double.toString()

双.toString()

Double.parseDouble(String s)

Double.parseDouble(String s)

回答by javaCity

You can directly invoke the "write" method to a double. For instance:

您可以直接调用 double 的“write”方法。例如:

double testDouble = 2.0;
bufferedOut.write(testDouble);

Then when reading the file as a string you can do the following to convert back to double:

然后在将文件作为字符串读取时,您可以执行以下操作以转换回双精度:

String line = //<what you read from file>;
double gotDouble = Double.parseDouble(line);

回答by PBL

double x = 3.14159; Any basic datatype can easily be converted to String by concatenating them with a String even if it is "" String value = "" + x; Most datatype Object counterpart have a parseXXX() method to translate a String into that datatype double y = Double.parseDouble(value);

双 x = 3.14159; 任何基本数据类型都可以通过将它们与字符串连接来轻松转换为字符串,即使它是 "" String value = "" + x; 大多数数据类型对象都有一个 parseXXX() 方法将字符串转换为该数据类型 double y = Double.parseDouble(value);

回答by kundan bora

Making Double as constant

使 Double 作为常量

public final Double DOUBLE = 20D;

making double as String -

使双倍作为字符串 -

String.valueOf(DOUBLE);