Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Which data structure is best suited for this?  RSS feed

 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm loading a binary file into a textfield. At the moment I'm using an ArrayList to do this, but I noticed it is very slow, and can only handle files less than one 1MB before it runs out of space, or theres a stack overflow. Could someone please recommend a more efficient way of doing this? Thank in advance.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, what kind of objects did you put into the ArrayList? I guess I'm also asking what's in your binary file? "Binary" seems to mean something different in nearly every post.

You should be able to get the size from the File object, create a byte[] array just that big and read the whole file from a Stream in one swell foop.

Since you're going to a textfield I wonder if you're not dealing with good old characters rather than bytes. If so you can read the whole file with a Scanner in one shot, too.
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By binary file I mean pretty much any file (text, image, exe). To read it in I'm using the DataInputStream with a bufferedInputStream, and using the readByte method for reading the data. I then use Integer.tostring(byteValue) to convert it to a string and add it to a String ArrayList. I'm still kinda iffy on the dfn of binary file, but from what I've read I think they're any files that are not plain text files, but I'm not a 100% sure on that
[ September 10, 2006: Message edited by: Bob Zoloman ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you really mean textfield (as in JTextField), or a JTextArea?

JTextComponents have a read(..) method which might suit

textArea.read(new FileReader(fileName),null);
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using a JTextField for displaying the output. At the moment I am just using the append method to write to the JTextField.
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you have file that you think is an image or an executable or something like that, and you're reading the file one byte at a time, promoting the byte to an int, creating a string representation of the numeric value, and adding the string to an ArrayList. And the file is a megabyte or more in length.

Well, I don't know what you're trying to accomplish, but I doubt you could do it any less efficiently. :-)
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol! I think your right
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be able to get the size from the File object, create a byte[] array just that big and read the whole file from a Stream in one swell foop.


What would be the max size that is permissible ???
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just profiled my code, and the big hot spot that is causing the big slows downs is the loop that appends the formatted string output to the textArea. Actually the loop that reads in the file data, converts the byte to string, and adds it to the arraylist is actually quite fast, and doesn't have much impact on the speed. It's just when I have to appends all those strings in the arraylist to the JTextField it goes a bit slow. I'll try the read() method

As for the max size of the file, I am aiming for around 5MB, or so.
[ September 11, 2006: Message edited by: Bob Zoloman ]
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I switched from append() to read(), and the difference is night and day. I can now read in files that are 5MB (still takes about 10 seconds), but the previous one took about 20 seconds on a 40KB file, so it's a very nice improvement For some weird reason the linewrap on the textField is not causing any slow downs anymore.... strange.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!