Solved

Efficiency in using File object and RandomAccessFile Object

Posted on 2001-06-21
5
684 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

752 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