ByteArray
をファイルに書き込むJavaプログラムを作成しました。その結果ByteArray
は、これら 3 つの ByteArrays の結果です。
- 最初の2 バイトは、
schemaId
短いデータ型を使用して表現したものです。 - 次に、次の8 バイトは、
Last Modified Date
長いデータ型を使用して表現したものです。 - 残りのバイトは、属性の実際の値である可変サイズにすることができます..
したがって、最初の行に上記のすべてのバイトが含まれる結果の ByteArray が含まれるファイルができました。次に、C++ プログラムからそのファイルを読み取り、ByteArray を含む最初の行を読み取り、それを分割する必要があります。上記のように、それに応じて結果の ByteArray を取得し、そこから my と実際の属性値を抽出できるようにしschemaId
ますLast Modified Date
。
私はすべてのコーディングを常に Java で行っており、C++ は初めてです... C++ でプログラムを記述してファイルを読み取ることはできますが、その ByteArray をどのように読み取ればよいかわかりません。上記のように分割します。
以下は、ファイルを読み取り、コンソールに出力する私の C++ プログラムです。
int main () {
string line;
//the variable of type ifstream:
ifstream myfile ("bytearrayfile");
//check to see if the file is opened:
if (myfile.is_open())
{
//while there are still lines in the
//file, keep reading:
while (! myfile.eof() )
{
//place the line from myfile into the
//line variable:
getline (myfile,line);
//display the line we gathered:
// and here split the byte array accordingly..
cout << line << endl;
}
//close the stream:
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
誰でもそれで私を助けることができますか?ありがとう。
アップデート
以下は、結果のByteArrayをファイルに書き込む私のJavaコードであり、同じファイルをc ++から読み戻す必要があります..
public static void main(String[] args) throws Exception {
String os = "whatever os is";
byte[] avroBinaryValue = os.getBytes();
long lastModifiedDate = 1379811105109L;
short schemaId = 32767;
ByteArrayOutputStream byteOsTest = new ByteArrayOutputStream();
DataOutputStream outTest = new DataOutputStream(byteOsTest);
outTest.writeShort(schemaId);
outTest.writeLong(lastModifiedDate);
outTest.writeInt(avroBinaryValue.length);
outTest.write(avroBinaryValue);
byte[] allWrittenBytesTest = byteOsTest.toByteArray();
DataInputStream inTest = new DataInputStream(new ByteArrayInputStream(allWrittenBytesTest));
short schemaIdTest = inTest.readShort();
long lastModifiedDateTest = inTest.readLong();
int sizeAvroTest = inTest.readInt();
byte[] avroBinaryValue1 = new byte[sizeAvroTest];
inTest.read(avroBinaryValue1, 0, sizeAvroTest);
System.out.println(schemaIdTest);
System.out.println(lastModifiedDateTest);
System.out.println(new String(avroBinaryValue1));
writeFile(allWrittenBytesTest);
}
/**
* Write the file in Java
* @param byteArray
*/
public static void writeFile(byte[] byteArray) {
try{
File file = new File("bytearrayfile");
FileOutputStream output = new FileOutputStream(file);
IOUtils.write(byteArray, output);
} catch (Exception ex) {
ex.printStackTrace();
}
}