因为直接写数据,数据格式没有转换为二进制字符。建议这样写:String trans = Double.toString(num)+"\n"; dfin.writeBytes(trans);
字符编码的问题,jdk上写的。他用的是8个字节,所以可能和你机子上默认的字符编码不一样,所以出错了,我刚才试了几次,用字符串才成功了的。 writeDouble public final void writeDouble(double v) throws IOException使用 Double 类中的 doubleToLongBits 方法将双精度参数转换为一个 long,然后按八字节数量将该 long 值写入该文件,先定高字节。写入从文件指针的当前位置开始。
可以看一下源代码嘛,这里写到文件里的就是二进制的里的, (八位的 二进制),可以考虑别的方法在文件里写吗 /** * Converts the double argument to a <code>long</code> using the * <code>doubleToLongBits</code> method in class <code>Double</code>, * and then writes that <code>long</code> value to the file as an * eight-byte quantity, high byte first. The write starts at the current * position of the file pointer. * * @param v a <code>double</code> value to be written. * @exception IOException if an I/O error occurs. * @see java.lang.Double#doubleToLongBits(double) */ public final void writeDouble(double v) throws IOException { writeLong(Double.doubleToLongBits(v)); }
dfin.writeBytes(trans);
writeDouble
public final void writeDouble(double v)
throws IOException使用 Double 类中的 doubleToLongBits 方法将双精度参数转换为一个 long,然后按八字节数量将该 long 值写入该文件,先定高字节。写入从文件指针的当前位置开始。
(八位的 二进制),可以考虑别的方法在文件里写吗
/**
* Converts the double argument to a <code>long</code> using the
* <code>doubleToLongBits</code> method in class <code>Double</code>,
* and then writes that <code>long</code> value to the file as an
* eight-byte quantity, high byte first. The write starts at the current
* position of the file pointer.
*
* @param v a <code>double</code> value to be written.
* @exception IOException if an I/O error occurs.
* @see java.lang.Double#doubleToLongBits(double)
*/
public final void writeDouble(double v) throws IOException {
writeLong(Double.doubleToLongBits(v));
}