• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Randomly accessing the file contents

September 25, 2007 //  by Krishna Srinivasan//  Leave a Comment

In this techincal tip, let us see how to access the contents of a file randomly instead of the traditional sequential access. Imagine that we want to search for a particular record in a file. If we prefer Sequential access in this case, then it will involve traversing over the file byte by byte and searching for the relevant record. This won’t be an ideal solution particularly if the size of the file is too big. So a mechanism must be there to randomly access the file contents in which case we can use java.io.RandomAccessFile class. The following code tries to open a file for random accessing,

also read:

  • Java Tutorials
  • Java EE Tutorials
  • Design Patterns Tutorials
  • Java File IO Tutorials
[code lang=”java”] RandomAccessFile file = new RandomAccessFile("employees.txt", "rw");
[/code]

The first argument passed to the RandomAccessFile constructor is the name of the file name and the second argument specifies the mode in which we want to operate on the open file. Passing "rw" means to open the file in read as well as in write mode. The following code snippet shows how to write some contents within the file.

[code lang=”java”] RandomAccessFile file = new RandomAccessFile("employees.txt", "rw");
writeEmployee(file, "John", 35, 54544.43d);
writeEmployee(file, "Jenny", 28, 34544.42d);
writeEmployee(file, "Joesph", 41, 64544.77d);
file.close();
[/code]

In the above code snippet we are trying to write some information (name, age and salary) about employee object. Here is the definition of the writeEmployee() method,

[code lang=”java”] static void writeEmployee(RandomAccessFile file, String name, int age,
double salary) throws Exception{
file.writeUTF(name);
file.writeInt(age);
file.writeDouble(salary);
}
[/code]

The methods RandomAccessFile.writeUTF(), RandomAccessFile.writeInt() and RandomAccessFile.writeDouble() are used to write string, integer and double data-types respectively. Now, let us look in the following section how to read the written contents back from the file,

[code lang=”java”] static void readEmployee(RandomAccessFile file) throws Exception{
String name = file.readUTF();
int age = file.readInt();
double salary = file.readDouble();
System.out.println("Name = " + name + ",
Age = " + age + ", Salary = " + salary);
}

[/code]

Till now, we have seen how to write and read sequentially using the RandomAccessFile object. Now, let us see the random access way using the methods getFilePointer() and seek(). The method getFilePointer() returns the current position of the file pointer. Consider the following code snippet,

[code lang=”java”] RandomAccessFile file = new RandomAccessFile("junk.txt", "rw");

file.writeInt(20);
System.out.println(file.getFilePointer());

file.writeDouble(12345.5454d);
System.out.println(file.getFilePointer());

file.close();

[/code]

We try to write an integer into the file. Since the size of an integer is 4 bytes, the file pointer would be 4 bytes away from the beginning of the file. So, the call to getFilePointer() will return 4. In the next statement, we tried to write some double value into the file and since its size is 8, the file pointer would have got traversed to 4 + 8 = 12. So the call to getFilePointer() will now return 12.

Now let us see the usage of the seek() method. Consider the following code snippet which writes even numbers into the file.

[code lang=”java”] RandomAccessFile file = new RandomAccessFile("evennumbers.txt", "rw");
int evenNums[] = {0, 2, 4, 7, 10};

for(int evenNum : evenNums){
file.writeInt(evenNum);
}
file.close();

[/code]

In the above code, we have stored the even numbers ranging between 0 and 10 in an array and then written them into a file using the enhanced for loop. Purposely, we have written the number 7 (which is not an even number) into the file. Let us correct this by making use of the seek() method. We know that writing an integer into the file will occupy 4 bytes and the number 7 is written after 3 * 4 = 12 bytes. The following code snippet forwards the file pointer to 12 bytes by using the seek() method and replaces the integer 7 with 8. Then it traverses over the file to read and print the contents to verify the new updated content.

[code lang=”java”] RandomAccessFile file = new RandomAccessFile("evennumbers.txt", "rw");
// Forward the file pointer to 6 bytes
file.seek(12);

// and write the content.
file.writeInt(8);

file.seek(0);
for(int index = 0; index < 5; index ++){
System.out.println(file.readInt());
}

file.close();

[/code]

Category: JavaTag: File IO

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Sorting Custom Types in Java
Next Post: Persisting Object State in Xml Format »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact