Click here to watch in Youtube :
https://www.youtube.com/watch?v=OeL1GLYw8-M&list=UUhwKlOVR041tngjerWxVccw
DataOutputStreamDemo.java
Outputhttps://www.youtube.com/watch?v=OeL1GLYw8-M&list=UUhwKlOVR041tngjerWxVccw
DataOutputStreamDemo.java
import java.io.DataOutputStream; import java.io.FileOutputStream; import java.io.IOException; public class DataOutputStreamDemo { public static void main(String[] args) throws Exception { DataOutputStreamDemo dataOutputStreamDemo = new DataOutputStreamDemo(); dataOutputStreamDemo.writePrimitiveData(); } public void writePrimitiveData() throws IOException { DataOutputStream dataOutputStream = null; FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream("data.txt"); dataOutputStream = new DataOutputStream(fileOutputStream); int[] intArray = { 100, 250, 440, 550, 900 }; for (int j : intArray) { dataOutputStream.writeInt(j); } System.out.println("Successfully written to \'data.txt\' file\n"); } finally { if (dataOutputStream != null) { /* * Closing a DataOutputStream will also * close the OutputStream instance to which * the DataOutputStream is writing. */ dataOutputStream.close(); } } } }
Successfully written to 'data.txt' file
data.txtDataInputStreamDemo.java
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; public class DataInputStreamDemo { public static void main(String[] args) throws Exception { DataInputStreamDemo dataInputStreamDemo = new DataInputStreamDemo(); dataInputStreamDemo.readPrimitiveData(); } public void readPrimitiveData() throws IOException { FileInputStream fileInputStream = null; DataInputStream dataInputStream = null; try { fileInputStream = new FileInputStream("data.txt"); dataInputStream = new DataInputStream(fileInputStream); while (dataInputStream.available() > 0) { int intValue = dataInputStream.readInt(); System.out.println(intValue); } } finally { if (dataInputStream != null) { /* * Closing a DataInputStream will also close * the InputStream instance from which the * DataInputStream is reading. */ dataInputStream.close(); } } } }
100 250 440 550 900
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_DataInput_and_Output_Stream_Example_App.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_DataInput_and_Output_Stream_Example_App
Bitbucket Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_DataInput_and_Output_Stream_Example_App
See also:
No comments:
Post a Comment