C++写入二进制文件时它的字节顺序是左地右高,而java则是左高右低,因此在读取数据时需要转换下。
转换代码如下:
public static int C2J_Int( int num ){
System.out.println( "num: "+Integer.toBinaryString(num) );
int value = 0;
int i0 = num >> 24 & 0xff;
int i1 = (num >> 8) & 0xff00;
int i2 = (num << 8) & 0xff0000;
int i3 = (num << 24) & 0xff000000;
value = i0|i1|i2|i3;
return value;
}
public static short C2J_Short( short num ){
int value = 0;
int i0 = (num >> 8) & 0xff;
int i1 = (num << 8) & 0xff00;
value = i0 | i1;
return (short)value;
}
C++代码部分:包含将数据写入文件和读取文件。
void writeData(){//写入数据
ofstream fout;
fout.open(filePath, ofstream::binary);
int n = 1000;
fout.write( (char *)&n, sizeof(n) );
short m = 10;
fout.write( (char *)&m, sizeof(m) );
char msg[100] = "你好吗我爱你哈啊 Hello C++ 哈哈啊啊哈哈啊哈哈";
int size = sizeof(msg);
fout.write( (char*)&size, sizeof(int) );
cout << "msg size: " << size << endl;
fout.write( (char *)&msg, size );
fout.close();
cout << "数据导入成功" << endl;
cout << "读取数据中..." << endl;
cout << "size: " << sizeof(int) << endl;
cout << "sizeof(str): "<< sizeof(msg) << endl;
cout << strlen(msg) << endl;
}
void readData(){//读取数据
ifstream fin;
fin.open( filePath, ifstream::binary );
int m = 0;
fin.read( (char *)&m, sizeof(int) );
cout << "m: " << m << endl;
short n;
fin.read( (char *)&n, sizeof(short) );
cout << "n: " << n << endl;
int size = 0;
fin.read( (char*)&size, sizeof(int) );
char msg[100] = "";//暂时不知道怎样让msg数组为动态数组通过size来初始化char数组大小,如果字串较长的话需要注意初始化
fin.read( (char *)&msg, sizeof(msg) );
cout << "msg: " << msg << endl;
fin.close();
}
JAVA 代码部分:读取C++写入的二进制流文件
import java.io.File;
import java.io.FileInputStream;
import java.io.DataInputStream;
public class Main {
public static void main( String args[] ){
readData();
}
public static void readData(){
System.out.println( "读取数据中..." );
try {
FileInputStream fis = new FileInputStream( new File("/789.dat") );
DataInputStream dis = new DataInputStream( fis );
System.out.println(dis.available());
int m = dis.readInt();
System.out.println(C2J_Int(m));
short n = dis.readShort();
System.out.println(C2J_Short(n));
String str = C2J_String( dis );
System.out.println( "str: "+str );
dis.close();
fis.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int C2J_Int( int num ){
System.out.println( "num: "+Integer.toBinaryString(num) );
int value = 0;
int i0 = num >> 24 & 0xff;
int i1 = (num >> 8) & 0xff00;
int i2 = (num << 8) & 0xff0000;
int i3 = (num << 24) & 0xff000000;
value = i0|i1|i2|i3;
return value;
}
public static short C2J_Short( short num ){
int value = 0;
int i0 = (num >> 8) & 0xff;
int i1 = (num << 8) & 0xff00;
value = i0 | i1;
return (short)value;
}
public static String C2J_String( DataInputStream dis ){
String result ="";
byte buffer[] = null;
try {
int size = C2J_Int(dis.readInt());//读取字符串长度
buffer = new byte[size];
dis.read(buffer);
result = new String( buffer, "gb2312" );
System.out.println( "result: "+result );
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}