Program to Write BYTE data type in file using DataOutputStream and Read BYTE from file using DataInputStream - java file IO




In this post we’ll write following programs -

  • Program 1 to Write BYTE data type in file using DataOutputStream
  • Program 2 to Read BYTE from file using DataInputStream - java file IO


Program 1 to Write BYTE data type in file using DataOutputStream >


import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class WriteByteToFileUsingDataOutputStream {
   public static void main(String... args) {
          FileOutputStream fos = null;
          DataOutputStream dos = null;
         
          try {
                 // create FileOutputStream for writing in file
                 fos = new FileOutputStream("c:/myFile.txt");
                 dos = new DataOutputStream(fos);
                 byte byteToBeWrittenInFile = 11;
                 // write byte data type in file using writeByte(byte) method
                 dos.writeByte(byteToBeWrittenInFile);
                
                 System.out.println("byte data type to be written in file = "
                              + byteToBeWrittenInFile);
                 System.out.println("byte data type has been written in file");
                 dos.flush(); // bytes in buffer are written in file
          } catch (IOException e) {
                 e.printStackTrace();
          } finally {
                 try {
                       if (dos != null){
                              dos.close(); //close DataOutputStream
                       }
                       if (fos != null){
                              fos.close(); //close FileOutputStream
                       }
                
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
   }
}
/* OUTPUT
byte data type to be written in file = 11
byte data type has been written in file
*/



Program 2 to Read BYTE from file using DataInputStream - java file IO >


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class ReadByteFromFileUsingDataInputStream {
   public static void main(String... args) {
          FileInputStream fis = null;
          DataInputStream dis = null;
         
          try {
                 // create FileInputStream for reading from file
                 fis = new FileInputStream("c:/myFile.txt");
                 dis = new DataInputStream(fis);
                 while (dis.available() > 0) {
                       // read byte data type from file using readByte() method.
                       byte byteReadFromFile = dis.readByte();
                       System.out.print("byte data type read from file = "
                                     + byteReadFromFile);
                 }
          } catch (IOException e) {
                 e.printStackTrace();
          } finally {
                 try {
                       if (dis != null){
                              dis.close(); //close DataInputStream
                       }
                       if (fis != null){
                              fis.close(); //close FileInputStream
                       }
                
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
   }
}
/* OUTPUT
byte data type read from file = 11
*/




RELATED LINKS>

Create File using createNewFile() method in java file IO

Program to Create Directory - Single and multiple (i.e. parent and child directories) in java file IO


Program to Identify File or Directory - in java file IO | And in java 7 using java.nio.file


Difference between FileInputStream and BufferedInputStream in java file IO


Program to Read text from file using BufferedReader's read() and readLine() methods in java file IO


Program to Write CHAR data type in file using DataOutputStream and Read CHAR from file using DataInputStream - java file IO


Program to Write INT data type in file using DataOutputStream and Read INT from file using DataInputStream - java file IO


No comments:

Post a Comment