// 在c++中,用二进制方式写入数值
int main(int argc, char* argv[]){
ofstream outfile("c:\\d1.dat");
float f = 12.5f;
outfile.write((char *)&f,sizeof(float));
f = 3332.5f;
outfile.write((char *)&f,sizeof(float));
f = 1442.5224f;
outfile.write((char *)&f,sizeof(float));
outfile.close();
return 0;
}
//在java中
public static void main(String[] args) throws Exception {
File f = new File("c:/d1.dat");
CppInputStream dis = new CppInputStream(new FileInputStream(f));
System.out.println(dis.readFloat());
System.out.println(dis.readFloat());
System.out.println(dis.readFloat());
}
// 参见java.io.DataInputStream
// C++写入的字节顺序是从低到高(左低到右高),
而java.io.DataInputStream读取的数据是从高到低(左高到右低)
// 所以需要自己改写一下
public class CppInputStream extends FilterInputStream {
public CppInputStream(InputStream in) {
super(in);
}
public final int read(byte b[]) throws IOException {
return in.read(b, 0, b.length);
}
public final int read(byte b[], int off, int len) throws IOException {
return in.read(b, off, len);
}
public final void readFully(byte b[]) throws IOException {
readFully(b, 0, b.length);
}
public final void readFully(byte b[], int off, int len) throws IOException {
if (len < 0)
throw new IndexOutOfBoundsException();
int n = 0;
while (n < len) {
int count = in.read(b, off + n, len - n);
if (count < 0)
throw new EOFException();
n += count;
}
}
public final int skipBytes(int n) throws IOException {
int total = 0;
int cur = 0;
while ((total < n) && ((cur = (int) in.skip(n - total)) > 0)) {
total += cur;
}
return total;
}
public final byte readByte() throws IOException {
int ch = in.read();
if (ch < 0)
throw new EOFException();
return (byte) (ch);
}
public final int readUnsignedByte() throws IOException {
int ch = in.read();
if (ch < 0)
throw new EOFException();
return ch;
}
public final short readShort() throws IOException {
int ch2 = in.read();
int ch1 = in.read();
if ((ch1 | ch2) < 0)
throw new EOFException();
return (short) ((ch1 << 8) + (ch2 << 0));
}
public final int readUnsignedShort() throws IOException {
int ch2 = in.read();
int ch1 = in.read();
if ((ch1 | ch2) < 0)
throw new EOFException();
return (ch1 << 8) + (ch2 << 0);
}
public final char readChar() throws IOException {
int ch2 = in.read();
int ch1 = in.read();
if ((ch1 | ch2) < 0)
throw new EOFException();
return (char) ((ch1 << 8) + (ch2 << 0));
}
public final int readInt() throws IOException {
int ch4 = in.read();
int ch3 = in.read();
int ch2 = in.read();
int ch1 = in.read();
if ((ch1 | ch2 | ch3 | ch4) < 0)
throw new EOFException();
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}
private byte readBuffer[] = new byte[8];
public final long readLong() throws IOException {
readFully(readBuffer, 0, 8);
return (((long) readBuffer[7] << 56) + ((long) (readBuffer[6] & 255) << 48)
+ ((long) (readBuffer[5] & 255) << 40) + ((long) (readBuffer[4] & 255) << 32)
+ ((long) (readBuffer[3] & 255) << 24) + ((readBuffer[2] & 255) << 16)
+ ((readBuffer[1] & 255) << 8) + ((readBuffer[0] & 255) << 0));
}
public final float readFloat() throws IOException {
return Float.intBitsToFloat(readInt());
}
public final double readDouble() throws IOException {
return Double.longBitsToDouble(readLong());
}
}
其他二进制数据的读取:
public static void readFile(String fileName){
File file = new File(fileName);
if(file.exists()){
try {
FileInputStream in = new FileInputStream(file);
DataInputStream dis=new DataInputStream(in);
byte[] itemBuf = new byte[20];
//市场编码
dis.read(itemBuf, 0, 8);
String marketID =new String(itemBuf,0,8);
//市场名称
dis.read(itemBuf, 0, 20);//read方法读取一定长度之后,被读取的数据就从流中去掉了,所以下次读取仍然从 0开始
String marketName =new String(itemBuf,0,20);
//上一交易日日期
dis.read(itemBuf, 0, 8);
String lastTradingDay = new String(itemBuf,0,8);
//当前交易日日期
dis.read(itemBuf, 0, 8);
String curTradingDay = new String(itemBuf,0,8);
//交易状态
dis.read(itemBuf, 0, 1);
String marketStatus = new String(itemBuf,0,1);
//交易时段数
short tradePeriodNum = dis.readShort();
System.out.println("市场代码:"+ marketID);
System.out.println("市场名称:"+ marketName);
System.out.println("上一交易日日期:"+ lastTradingDay);
System.out.println("当前交易日日期:"+ curTradingDay);
System.out.println("当前交易日日期:"+ curTradingDay);
System.out.println("交易状态:"+ marketStatus);
System.out.println("交易时段数:"+ tradePeriodNum);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//close
}
}
}