[英]A double written by Java is not recognized by Ruby
I am using an algorithms written in Java with such an output feature: 我正在使用用Java编写的具有这样的输出功能的算法:
DataOutputStream out = new DataOutputStream(new FileOutputStream("lalala.txt"));
out.writeChars(string_double_copy_B[0] + "/" + string_double_copy_B[1] + "\t");
out.writeChars(String.valueOf(corrValue));
out.writeChars("\n");
corrValue
is a double. corrValue
是双corrValue
。 string_double_copy_B
is a string array. string_double_copy_B
是一个字符串数组。
After that I try to read a produced file with ruby. 之后,我尝试使用ruby读取生成的文件。 I have the following code. 我有以下代码。
input=File.open("lala.txt","r")
genes=[]
input.each_line{|li|
keys=li.split("\t")
length=keys.length
puts(keys[length-1].inspect)
}
and I get such an output: 我得到这样的输出:
"\u00000\u0000.\u00009\u00000\u00000\u00002\u00003\u00003\u00006\u00006\u00001\u00005\u00003\u00006\u00000\u00002\u00001\u00005\u0000"
Something wring happens when java writes a double into a file. java将double写入文件时,发生了一些事情。 I have tried as well with PrintWriter out = new PrintWriter(new FileOutputStream("lala...))
It shows me the same thing. 我也尝试了PrintWriter out = new PrintWriter(new FileOutputStream("lala...))
它向我展示了同样的事情。
How can I fix it? 我该如何解决? What is then the appropriate way of writing in java in order to get a normal double? 为了得到普通的double,用Java编写的合适方法是什么?
kik@kik-VirtualBox:~/Desktop$ od -t a -t x1 stemcells09_negative_werte.txt
0000000 nul 1 nul 0 nul 5 nul 3 nul _ nul a nul t nul /
00 31 00 30 00 35 00 33 00 5f 00 61 00 74 00 2f
0000020 nul R nul F nul C nul 2 nul ht nul 2 nul 0 nul 3
00 52 00 46 00 43 00 32 00 09 00 32 00 30 00 33
0000040 nul 6 nul 9 nul 6 nul _ nul s nul _ nul a nul t
00 36 00 39 00 36 00 5f 00 73 00 5f 00 61 00 74
0000060 nul / nul R nul F nul C nul 2 nul ht nul 0 nul .
00 2f 00 52 00 46 00 43 00 32 00 09 00 30 00 2e
0000100 nul 9 nul 0 nul 3 nul 1 nul 6 nul 9 nul 9 nul 6
00 39 00 30 00 33 00 31 00 36 00 39 00 39 00 36
You write binary representation of String instead of String itself. 您编写String的二进制表示形式,而不是String本身。 Use Writer
instead to write Strings: 改用Writer
编写字符串:
Writer out = new FileWriter("lalala.txt");
out.write(string_double_copy_B[0] + "/" + string_double_copy_B[1] + "\t");
out.write(String.valueOf(corrValue));
out.write("\n");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.