Problem - How you can read primitive data types from a file? Solution : java.io.RandomAccessFile behaves like a large array of bytes stored in the file system. There is a kind of cursor, or index into the implied array, called the file pointer. Prerequisite:- You must create Write Primitive File first to run this program. Program :Step 1 - Coding Create a text file c:/sunilos/ReadPrimitiveFile.java and copy below contents.import java.io.RandomAccessFile; /** * A program to read primitive data types in a file. */ public class ReadPrimitiveFile { public static void main(String[] args) throws Exception { long dataPosition=0; long data = 0; RandomAccessFile raf = new RandomAccessFile("c:/sunilos/sunilosfile.dat","r"); dataPosition = raf.readLong(); System.out.println("dataPosition : " +dataPosition); raf.seek(dataPosition); data = raf.readInt(); raf.close(); System.out.println("The dat is : " +data); } } Step 2 - Deployment
Step 3 - Testing
OutputdataPosition : 32 The dat is : 1114399080 <<Previous | Next>> |