由于外部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