关于JAVA 读各种数据的问题 我现在已有自己写好的二进制文件里面有我按要求的顺序写入的各种数据,包括字符型,整型,浮点型等等我现在想用DataInputStream 的对象将里面的数据按自己的形式读取出来请问有什么办法吗~谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 DataInputStream直接就可以读的啊。如果你的数据是用Java写的,那直接读就没问题。如果是用C/C++写的,那可能字节是反的。比如C/C++的整型是从低字节到高字节保存的,而Java是从高到低。另外一定就是要注意数据的长度。Java的Long是8字节,C的long是4字节我现在没时间给你写示例。如果你还没明白,给我发站内信吧。 import Java.io.DataInputStream;import java.io.DataOutputStream;import java.io.EOFException;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class DataIODemo{ public static void main(String[] args) throws IOException { DataOutputStream out = new DataOutputStream(new FileOutputStream( "demoFile.txt"));/** double[] d= { 0.1, 0.2, 1.1, 3.9, 4.5 }; //浮点型 int[] a = { 12, 8, 13, 29, 50 }; //整形 char[] ch = { 'a','b','c'}; //字符型 for (int i = 0; i < d.length; i++) { out.writeDouble(d[i]); out.writeChar('\t'); out.writeInt(a[i]); out.writeChar('\t'); out.writeChars(ch[i]); out.writeChar('\n'); } out.close(); */ DataInputStream in = new DataInputStream(new FileInputStream( "demoFile.txt")); //直接读取 double d; int a; char ch; try { while (true) { d= in.readDouble(); a = in.readInt(); ch = in.readChar(); System.out.println( d); System.out.println( a); System.out.println( ch); } } catch (EOFException e) { } in.close(); }} 如何统计一个域名下面的所有二级域名 如何用java写出菜单 java的正规表达式中如何让点表示点字符,而不是任意一个字符? StringTokenizer无法解析连续两个\n吗? 求助.我的helloworld程序出现了错误提示 问题出在那里? 我想用方向键控制图形(方块)移动. 在setLayout(new GridLayout(1,2))调用之后,怎么设置第一列的宽度? 关于JDBC的小问题!请各位大哥们指导一下! 设置变量和获值类 问一个基本问题,在下初学 在设计文本阅读器的时候,为什么不能显示文本的内容呢? 如何提高网站的浏览度呢?
如果你的数据是用Java写的,那直接读就没问题。
如果是用C/C++写的,那可能字节是反的。比如C/C++的整型是从低字节到高字节保存的,而Java是从高到低。
另外一定就是要注意数据的长度。Java的Long是8字节,C的long是4字节我现在没时间给你写示例。如果你还没明白,给我发站内信吧。
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class DataIODemo{
public static void main(String[] args) throws IOException {
DataOutputStream out = new DataOutputStream(new FileOutputStream(
"demoFile.txt"));
/**
double[] d= { 0.1, 0.2, 1.1, 3.9, 4.5 }; //浮点型
int[] a = { 12, 8, 13, 29, 50 }; //整形
char[] ch = { 'a','b','c'}; //字符型
for (int i = 0; i < d.length; i++) {
out.writeDouble(d[i]);
out.writeChar('\t');
out.writeInt(a[i]);
out.writeChar('\t');
out.writeChars(ch[i]);
out.writeChar('\n');
}
out.close();
*/
DataInputStream in = new DataInputStream(new FileInputStream(
"demoFile.txt")); //直接读取 double d;
int a;
char ch; try {
while (true) {
d= in.readDouble();
a = in.readInt();
ch = in.readChar();
System.out.println( d);
System.out.println( a);
System.out.println( ch);
}
} catch (EOFException e) {
}
in.close();
}
}