由於外部C++應用程序必須讀取此文件,因此我必須在文件中寫入4bytes,代表little endian中的整數(java use big endian)。我的代碼不會在te文件中寫入任何內容,但de buffer中有數據。爲什麼? 我funcion:在小端中編寫一個整數
public static void copy(String fileOutName, boolean append){
File fileOut = new File (fileOutName);
try {
FileChannel wChannel = new FileOutputStream(fileOut, append).getChannel();
int i = 5;
ByteBuffer bb = ByteBuffer.allocate(4);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.putInt(i);
bb.flip();
int written = wChannel.write(bb);
System.out.println(written);
wChannel.close();
} catch (IOException e) {
}
}
我的電話:
copy("prueba.bin", false);
不要忽略異常。在'catch'塊中寫入'e.printStackTrace()' – alaster
我試過這段代碼,它在文件中寫了4個字節 – Evans
這可能是非同步IO的問題。寫入方法不會阻止,因此無法保證在您關閉時它會完成。並且關閉力量阻止了線程立即退出,這可能會導致異常中止寫入操作,而您忽略了該異常。 – jpm