Write an integer in little endian
bytebuffer, endianness, io, java, nio
Solution
When you don't know why something failed, it is a bad idea to ignore exceptions in an empty try-catch block.
Odds are excellent that you are running the program in an environment where the file cannot be created; however, the instructions you gave to handle such an exceptional situation is to do nothing. So, odds are you have a program that attempted to run, but failed with some reason, which was handled by not even displaying the reason to you.
try this
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) {
// this is the new line of code
e.printStackTrace();
}
}
And I'll bet you find out why it doesn't work right away.
Problem
i have to write in a file 4bytes representing an integer in little endian (java use big endian) because an external c++ application have to read this file. My code don't write anything in te file but de buffer has data inside. why? my 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) { } } ``` my call: ``` copy("prueba.bin", false); ```