Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Lightning fast file reader?



On April 5, 2004, at 2:36 AM, Joe Sam Shirah wrote:

At those file sizes and for your purposes, I don't think NIO is going to
help much, if at all.

Yes, agreed. It seems the overhead of setting up channels/memory mapping negates NIO's performance advantages when working with small files.

Jack Shirazi goes to the extreme of writing a custom character converter
in "Java Performance Tuning". It's interesting to read and cut about 50% in
his samples, but I don't think that's really workable for most apps.

I tried some custom ByteArrayOutputStreams with synchronization turned off, not much help.

Your issue seems to me to be one of user perception. If they can see
data fast, they're happy - right? Doing less always takes less time than
doing more.

This is excellent advice! "Always do less." I've implemented a restriction on the buffer size:

// Limit preview to 32 K characters
int size = Math.min((int)file.length(), 32768);
byte[] buffer = new byte[size];

and filling the text area with just a few lines through which to scroll. It works well! Thank you for your help, Joe Sam!

Craig

P.S. After much experimentation, this is the fastest way I've found to read the entire contents of a small file:

public static String readFile(String filename)
{
String s = "";
FileInputStream in = null;
try
{
File file = new File(filename);
int size = (int) file.length();
if (size > 0)
{
byte[] buffer = new byte[size];
in = new FileInputStream(file);
in.read(buffer, 0, size);
s = new String(buffer, 0, size, "ISO8859_1");
in.close();
}
}
catch (FileNotFoundException fnfx)
{
System.err.println("File not found: " + fnfx);
}
catch (IOException iox)
{
System.err.println("I/O problems: " + iox);
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException ignore) {}
}
}
return s;
}
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >Lightning fast file reader? (From: Craig Mattocks <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.