Solved

Efficiency in using File object and RandomAccessFile Object

Posted on 2001-06-21
5
675 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
Comment Utility
Is it because of RandomAccessFile traffics in bytes rather than characters.
0
 
LVL 1

Expert Comment

by:tonus
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry for late reply, but thanks alot for your help, they are very helpful
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countX 22 68
How Complex Is This Java Course ? 9 62
array220 challenge 8 44
Passing list of object to Oracle Database Procedure 3 40
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 …

771 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

10 Experts available now in Live!

Get 1:1 Help Now