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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
/* Read double from file using DataInputStream This Java example shows how to read a Java double primitive value from file using readDouble method of Java DataInputStream class. */ import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class ReadDoubleFromFile { public static void main(String[] args) { String strFilePath = "C://FileIO//readDouble.txt"; try { //create FileInputStream object FileInputStream fin = new FileInputStream(strFilePath); /* * To create DataInputStream object, use * DataInputStream(InputStream in) constructor. */ DataInputStream din = new DataInputStream(fin); /* * To read a Java double primitive from file, use * byte readDouble() method of Java DataInputStream class. * * This method reads 8 bytes and returns it as a double value. */ double d = din.readDouble(); System.out.println("Double : " + d); /* * To close DataInputStream, use * void close() method. */ din.close(); } catch(FileNotFoundException fe) { System.out.println("FileNotFoundException : " + fe); } catch(IOException ioe) { System.out.println("IOException : " + ioe); } } } |
Add Comment