Problem : How you can read/write to primitive 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. Program :Step 1 - CodingCreate a text file c:/sunilos/WritePrimitiveFile.java and copy below contents.import java.io.RandomAccessFile; /** * A program to write primitive data types in a file. */ public class WritePrimitiveFile { public static void main(String[] args) throws Exception{ long dataPosition = 0; RandomAccessFile in = new RandomAccessFile("c:/sunilos/sunilosfile.dat","rw"); in.writeLong(0); in.writeChars("blahblahblah"); dataPosition = in.getFilePointer(); in.writeBytes("Blahblahblah"); in.seek(0); in.writeLong(dataPosition); in.close(); } } Step 2 - Deployment
Step 3 - Testing
<<Previous | Next>> |