Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.io.DataOutputStream; import java.io.FileOutputStream; public class Main { public static void main(String[] args) { try { String content = "Data Input Stream Examnple.\n"; byte[] array = content.getBytes(); FileOutputStream fos = new FileOutputStream("file.txt"); DataOutputStream dos = new DataOutputStream(fos); for (byte b : array) { dos.write(b); } dos.close(); fos.close(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } } |
The Java.io.DataOutputStream class lets an application write primitive Java data types to an output stream in a portable way.