Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to get Filesize  RSS feed

 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have java application. I can get the name of file but how to get the size of that file ?
Thanks,
angela
 
Sumeet Pandit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If You want to know the length of the file then use .lenght() method of File class which return long.
ok.
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you please give me simple example showing how to get the size of file?
Thanks,
Angela
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Code is here:
File f = new File("Test.txt");
long size = f.length();
This will give me size of the file but if it is 1 KB. If it is more than 1KB, it will give me 0. Why???
Thanks,
Angela
 
Siva Prasad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
If you read the description of the method length(), it clearly says that the return value of length() will be 0L(long value) if it cannot find the specified file name on the disk. So in your example there may not be such file called Test.txt
If you need more clarification, then use exists() method.
cheers
sivaPrasad
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!