Mega Code Archive

 
Categories / Java / File Input Output
 

Use RandomAccessFile class

import java.io.RandomAccessFile; public class Main {   public static void main(String[] args) throws Exception {     RandomAccessFile raf = new RandomAccessFile("books.dat", "rw");     String books[] = new String[5];     books[0] = "A";     books[1] = "B";     books[2] = "C";     books[3] = "D";     books[4] = "E";     for (int i = 0; i < books.length; i++) {       raf.writeUTF(books[i]);     }     raf.seek(raf.length());     raf.writeUTF("Servlet & JSP Programming");     raf.seek(0);     while (raf.getFilePointer() < raf.length()) {       System.out.println(raf.readUTF());     }   } }