9 Replies Latest reply: Nov 28, 2009 7:13 PM by 807580

# Receiving the percentage of a file loaded?

Hello everyone!
I'm currently struggeling more than a bit, trying to figure out, how I can make my UI tell me how many percentages of my file that has been loaded..
I want it to say like: "Loading cache - PERCENTAGE%" and of course I want PERCENTAGE to be replaced with a increasing number.. You know, I would like it to start at '0%' and then go all the way up. All I need is some 'getPercentage()' method or something like that, because I already got the JLabel figured out, but my own getPercentage() method didn't help at all.. :/
So if you could just show me how to do this, I would REALLY appreciate it! :)

• ###### 1. Re: Receiving the percentage of a file loaded?
What specifically are you having trouble with?

Do you know how to calculate the percentage of a file that's been loaded outside of Java?
• ###### 2. Re: Receiving the percentage of a file loaded?
Nope, what I need help with is the 'formula' on how to do this...
• ###### 3. Re: Receiving the percentage of a file loaded?
Vimsie wrote:
Nope, what I need help with is the 'formula' on how to do this...
Then this is not a Java question. It's basic math.

Your gas tank is empty, and you're filling it up. Do you know how to figure out what percent full it is a a given point? If not, you need some serious review of math fundamentals, and this site it not the appropriate venue for that.
• ###### 4. Re: Receiving the percentage of a file loaded?
Vimsie wrote:
So if you could just show me how to do this, I would REALLY appreciate it! :)
double percentage = 100.0 * {bytes loaded so far}/{size of file in bytes}

That tells you the formula. It doesn't tell you how to get the to values. Those depend on what you mean by "loaded".
• ###### 5. Re: Receiving the percentage of a file loaded?
Thanks a lot! :)
This worked:
``````File cache = new File("./cache/main_file_cache.DAT");
int length = (int)cache.length();
int available = dataInput.available();
int index = length - available;
percentage = 100 * index / length;``````
So yeah thanks a lot! :D
• ###### 6. Re: Receiving the percentage of a file loaded?
Just making sure I'm not missing something here. The problem really was that you didn't know that "percent" is calculated as 100 * done / total? Like if you got 35 out of 40 on a test, you couldn't calculate that that's 87.5%?
• ###### 7. Re: Receiving the percentage of a file loaded?
Don't use available(). It only tells you how much can be read without blocking. It may happen to match up with total remaining bytes to read in your testing, but it's not guaranteed to. There's a better way to do this.
• ###### 8. Re: Receiving the percentage of a file loaded?
Vimsie wrote:
..how many percentages of my file that has been loaded..
See:
1) [ProgressMonitorInputStream JavaDoc|http://java.sun.com/javase/6/docs/api/javax/swing/ProgressMonitorInputStream.html].
2) [How to Use Progress Monitors|http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html#monitors] in the Java Tutorial.
• ###### 9. Re: Receiving the percentage of a file loaded?
Do you want to show the percentage of files loaded? Or do you want to show the percentage of a file's data that's loaded?

Because, if it's the latter - I'm not quite sure. Otherwise, it's quite easy.

Edit: Oops! I hadn't realized that it was already answered.

Edited by: b1nary on Nov 28, 2009 5:12 PM