[英]Write float numbers to File
I have floating point numbers in two files. 我在两个文件中有浮点数。 I want to write them in to single file.
我想将它们写入单个文件。 When I open the file after writing, I cannot see the floating point numbers.
写入后打开文件时,看不到浮点数。 I can see some machine code or something.
我可以看到一些机器代码或其他东西。 Here is my code
这是我的代码
import java.io.*;
import java.util.*;
public class F
{
public static void main(String args[]) throws Exception
{
FileInputStream fr1 = new FileInputStream("distance.txt");
BufferedReader br1 = new BufferedReader(new InputStreamReader(fr1));
FileInputStream fr2 = new FileInputStream("price.txt");
BufferedReader br2 = new BufferedReader(new InputStreamReader(fr2));
FileOutputStream fw1 = new FileOutputStream("file1.txt");
BufferedOutputStream bos=new BufferedOutputStream(fw1);
DataOutputStream dos=new DataOutputStream(bos);
String s1,s2;
while(((s1 = br1.readLine()) != null) && ((s2=br2.readLine())!=null))
{
float a1=Float.parseFloat(s1);
float a2=Float.parseFloat(s2);
a1=a1*10;
a2=a2*10000;
System.out.println(a1+" "+a2);
dos.writeFloat(a1);
dos.writeFloat(a2);
}
}
}
You are writing the float in a binary format. 您正在以二进制格式编写浮点数。
Note: DataInput/OutputStream is for binary formats and Reader/Writers are for text formats. 注意:DataInput / OutputStream用于二进制格式,而Reader / Writers用于文本格式。
I suggest you use a PrintWriter to write text. 我建议您使用PrintWriter编写文本。
BTW You can use FileReader instead of FileInputStream and InputStreamReader. 顺便说一句,您可以使用FileReader代替FileInputStream和InputStreamReader。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.