Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Efficiency in using File object and RandomAccessFile Object

Posted on 2001-06-21
5
Medium Priority
?
699 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 150 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

927 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