This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes FileInputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "FileInputStream" Watch "FileInputStream" New topic
Author

FileInputStream

Steven Smith
Greenhorn

Joined: Dec 12, 2005
Posts: 14
What's the best way to get the byte lenght of a FileInputStream? I read in the bytes to an array list then get the size of the array list. I'm wondering if there is a better way?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

File.length()


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Brian Mozhdehi
Ranch Hand

Joined: Aug 17, 2006
Posts: 81
You can also do the following:

FileInputStream aFileInputStream = new FileInputStream("filename");

//initialize a byte array with the size in bytes of the file
//FileInputStream.available() returns that size
byte[] bb = new byte[aFileInputStream.available()];

aFileInputStream.read(bb);
aFileInputStream.close();

You should of course use Try/Finally blocks around all this to ensure that any exception still results in the aFileInputStream.close() being executed
Steven Smith
Greenhorn

Joined: Dec 12, 2005
Posts: 14
length() returns long.
[ August 21, 2006: Message edited by: Steven Smith ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

Brian, Available Doesnt Do What You Think It Does
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Steven]: length() returns long.

Yep. Files can be larger than Integer.MAX_VALUE in size (2 GB), so an int is not sufficient here. If a file is this big, you can't possibly store all the content in a byte[] anyway. And you would probably get an OutOfMemoryError for a considerably smaller file anyway. You can increase the JVM memory settings, but how much RAM do you have, after all? What I would do is something like this:

Actually I would probably prefer to rewrite the subsequent code so that there is no need to have all the content in a single byte[] at one time. E.g. using streams. But it's hard to say more about that without knowing what sort of processing you're doing with the data here. If you are reasonably sure that your files will always be small enough that memory will not be a problem, then go ahead and load everything into a byte[]. But if the files may be very large, find another way to do this, probably using streams.
[ August 21, 2006: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Steven Smith
Greenhorn

Joined: Dec 12, 2005
Posts: 14
Originally posted by Jim Yingst:

Actually I would probably prefer to rewrite the subsequent code so that there is no need to have all the content in a single byte[] at one time. E.g. using streams.


Thanks Jim, using streams is actually what I'd like to do, since file may be huge. Without getting into details, need the length in bytes. Any hints to get started?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by Steven Smith:
Without getting into details, need the length in bytes. Any hints to get started?
The length of what?

If you need to know the number of bytes in the file, then the very first reply in this thread tells you how to find the number of the bytes in a file. Did you just overlook that, or do you have a question about it?
Steven Smith
Greenhorn

Joined: Dec 12, 2005
Posts: 14
Originally posted by Paul Clapham:
The length of what?

If you need to know the number of bytes in the file, then the very first reply in this thread tells you how to find the number of the bytes in a file. Did you just overlook that, or do you have a question about it?


Using a stream, not simply File.length().
Steven Smith
Greenhorn

Joined: Dec 12, 2005
Posts: 14
Originally posted by Steven Smith:

Without getting into details, need the length in bytes.


May have worded this wrong. Need length using a stream.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Um, why? Using File.length() for this is really easy. Unless you don't actually have a file at all to look at. If you must use a stream, it's not generally possible to know the total length until after you have read all the content - just add up the total bytes read as you go. Unless wherever-you're-getting-the-data-from provides some alternate way to get the info without reading from the stream. I think we need more info to make sense of what you're doing here...
[ August 21, 2006: Message edited by: Jim Yingst ]
Steven Smith
Greenhorn

Joined: Dec 12, 2005
Posts: 14
Originally posted by Jim Yingst:
I think we need more info to make sense of what you're doing here...


Yes, sorry my fault. I was trying to be brief and not get detailed, but it made more confusion. Anyway, I got it working now by simply reading the file in a while loop and totaling the bytes. Thanks
[ August 21, 2006: Message edited by: Steven Smith ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FileInputStream
 
Similar Threads
randomly iterating?
How to convert image to bytearray in MIDlet
Breaking up files, please help!
Yet another upload file question...
How to create a List from an Array?