Show pageBacklinksBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== Byte Array to File ====== ===== Classic Way ===== <code> public static void main(String[] args) { byte[] b = "ABCDEFGHIJK".getBytes(); FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream("./out.txt"); fileOutputStream.write(b); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fileOutputStream != null){ try { fileOutputStream.close(); } catch (IOException ignored) { //DO NOT CARE... } } } } </code> ===== Better Way Since Java 1.7 with try(resource){}===== We do not have to close the resource, Java will handle it for us. <code> public static void main(String[] args) { byte[] b = "ABCDEFGHIJK".getBytes(); try (FileOutputStream fileOutputStream = new FileOutputStream("./out.txt")) { fileOutputStream.write(b); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } </code> java/byte_to_file.txt Last modified: 2020/06/15 11:50by chongtin