Solved

Efficiency in using File object and RandomAccessFile Object

Posted on 2001-06-21
5
679 Views
Last Modified: 2010-07-27
I have 2 piece of code that reads in data from a text file.One of them uses a File Object and the other uses RandomFileAccess as follows:



(1)File Object code:

filename = "c:\\data.dat";

File loaddata = new File(filename)

try
{
    if ( !userdefinefile.exists())
    {
     return;
    }
    else
    {
     BufferedReader br = new BufferedReader(new FileReader(userdefinefile));
                   
        while ( br.readLine() != null)
     {          
            //code to process each line until end of file
        }
         

}catch (Exception ex) {}




(2)RandomAccessFile object code:

filename = "c:\\data.dat"

try
{
    RandomAccessFile loaddata = new RandomAccessFile(filename, "rw");

    while( loaddata.readLine() != null)
    {
        //code to process each line read.
    }
}
catch( Exception ex ){}




The problem is that the code using the RandomAccessFile object seem to be slower that the code using File Object
.Can anybody tell why this is so? And how can I improve the efficiency of using RandomAccessFile Object readLin() method?
0
Comment
Question by:jerntat
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:tonus
ID: 6217139
Is it because of RandomAccessFile traffics in bytes rather than characters.
0
 
LVL 1

Expert Comment

by:tonus
ID: 6217145
Again you can read the entire file at one stratch using RAF


Here is the code

  RandomAccessFile loaddata = new RandomAccessFile(filename, "rw");

  Byte[] byteArray = new Byte[(int)loaddata.length()];

loaddata.readFully(byteArray);
0
 

Author Comment

by:jerntat
ID: 6217150
but you are still reading in the same amount of data whether in byte or chracters,shouldnt both method at least uses the same amount of time

I only wanted to read a small part of the file at a time because i dont want to use too much memory.
0
 
LVL 3

Accepted Solution

by:
dnoelpp earned 50 total points
ID: 6371337
Just a comment:

No, a character is two bytes (since it's Unicode).

And as for efficiency, Java IO is not very efficient. I really don't know very much why you found a difference, so I can't help you here. I can give you some hints for exploring the difference.

Optimizing Java IO is very helpful because with some effort you can gain savings of several hundred percents in speed. Using a BufferedReader can accelerate reading by a factor of 10 and more. Using a big buffer (several kB till a MB) can make a difference, too, but at the expense of memory. Using a Reader on an not Unicode file can slow things down very much because of the need to convert the ASCII (or an other encoding as say, Latin1) to Unicode. If you don't need Strings, use Streams, not Readers.

Use profiling and compare the results because optimizing always gives surprising results (an optimization doesn't work at all and slows down and a small change elsewhere has an unexpected big benefit).

Cheers!
0
 

Author Comment

by:jerntat
ID: 6469267
Sorry for late reply, but thanks alot for your help, they are very helpful
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java.io.BufferedReader   readLine()  //returns the entire file ? 4 60
Basic Java Case or If-Else statement... 3 43
print map entry 34 55
diffSum example 4 26
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now