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




In this post we’ll write following programs -

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




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

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



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


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




RELATED LINKS>


Copy a file in java 2 in ways


Program to Hide and unHide File or Directory (by executing CMD commands in java program) till java6 - file IO


Program on How to hide and unHide file or Directory in java 7 using java.nio.file

Program to Read text from file using FileInputStream and Try with resource provided in java 7


Program to Encrypt and decrypt text using password - Write encrypted text to file | Read encrypted text from file in java



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