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




In this post we’ll write following programs -

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



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


import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class WriteLongToFileUsingDataOutputStream {
   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);
                 long longToBeWrittenInFile = 14l;
                 // write long data type in file using writeLong(long) method
                 dos.writeLong(longToBeWrittenInFile);
                
                 System.out.println("long data type to be written in file = "
                              + longToBeWrittenInFile);
                 System.out.println("long 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
long data type to be written in file = 14
long data type has been written in file
*/



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


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class ReadLongFromFileUsingDataInputStream {
   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 long data type from file using readLong() method.
                       long longReadFromFile = dis.readLong();
                       System.out.print("long data type read from file = "
                                     + longReadFromFile);
                 }
          } 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
long data type read from file = 14
*/


eEdit
Must read for you :