Solved

Efficiency in using File object and RandomAccessFile Object

Posted on 2001-06-21
5
683 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

830 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