The Artima Developer Community
Sponsored Link

Java Answers Forum
How to find size of a file (in terms of Bytes, KB, MB & GB..) using Java -

3 replies on 1 page. Most recent reply: Apr 4, 2006 1:32 PM by Arul Murugan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Arul Murugan

Posts: 7
Nickname: appaney
Registered: Mar, 2006

How to find size of a file (in terms of Bytes, KB, MB & GB..) using Java - Posted: Mar 10, 2006 1:01 PM
Reply to this message Reply
Advertisement
Hi anybody, how to find the file size using Java, already we have the length() method to find the length of a file, which shows the length in bytes even for the larger files which are in mega bytes. Is there any possible way/logic in java to find the size of a file.

Your immidiate responses are most welcome

Thanks in advance.


Renju Raveendran.P

Posts: 1
Nickname: kochu
Registered: Oct, 2005

Re: How to find size of a file (in terms of Bytes, KB, MB & GB..) using Java - Posted: Mar 11, 2006 12:59 AM
Reply to this message Reply
Hi,
You are getting the size in bytes with length() methode no? Then by dividing it by 1000 you can obtain the size in kb. I have done like that only. If you came to know about a methode by which size in MB or GB can be directly obtained then please let me know.

Regards,
Cochu.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: How to find size of a file (in terms of Bytes, KB, MB & GB..) using Jav Posted: Mar 13, 2006 1:11 AM
Reply to this message Reply
Are you sure about '1000'? Shouldn't it be '1024' instead?

Arul Murugan

Posts: 7
Nickname: appaney
Registered: Mar, 2006

Re: How to find size of a file (in terms of Bytes, KB, MB & GB..) using Java - Posted: Apr 4, 2006 1:32 PM
Reply to this message Reply
Hi all, I wrote the code to find the size of the file.
This code actually find the content size of the file not the file size in the disk.


import java.io.File;
import java.util.*;
/***************************************************************** ******************************
* This program finds the actual content size of the file not the size of the file in the disk.
* Eg: Check the 1 Byte text file for its actual content size and the size of that same file in the
* disk.
******************************************************************************* ****************/

public class FindFileSize
{
double fileSizeKB;



public FindFileSize()
{
/**Specify the location and name of the file whose size is to be found **/

File f = new File("C:\\arulTest\\arul2.zip");

String fileLength = String.valueOf(f.length());
int fileLengthDigitCount = fileLength.length();
double fileLengthLong = f.length();
double decimalVal = 0.0;
String howBig = "";

System.out.println("fileLengthDigitCount is..."+fileLengthDigitCount);

if(f.length()>0)
{
if(fileLengthDigitCount < 5)
{
fileSizeKB = Math.abs(fileLengthLong);
howBig = "Byte(s)";
}
else if(fileLengthDigitCount >= 5 && fileLengthDigitCount <=6)
{
fileSizeKB = Math.abs((fileLengthLong/1024));
howBig = "KB";
}
else if(fileLengthDigitCount >= 7 && fileLengthDigitCount <= 9)
{
fileSizeKB = Math.abs(fileLengthLong/(1024*1024));
howBig = "MB";
}
else if(fileLengthDigitCount >9)
{
fileSizeKB = Math.abs((fileLengthLong/(1024*1024*1024)));
decimalVal = fileLengthLong%(1024*1024*1024);
howBig = "GB";
}
}
System.out.println("....bytes....."+fileSizeKB);
String finalResult = getRoundedValue(fileSizeKB);
System.out.println("\n....Final Result....."+finalResult+" "+howBig);
}

private String getRoundedValue(double decimalVal)
{
System.out.println("\nThe first call......."+decimalVal);

long beforeDecimalValue = decimalTokenize(decimalVal,1);
long afterDecimalValue = decimalTokenize(decimalVal,2);
long decimalValueLength = String.valueOf(afterDecimalValue).length();
long dividerVal = divider(decimalValueLength-1);
long dividedValue = afterDecimalValue/dividerVal;
String finalResult=String.valueOf(beforeDecimalValue)+"."+String.valueOf(dividedValue) ;

System.out.println("\nfinalResult......."+finalResult);

return finalResult;
}

private long divider(long argLength)
{
long varDivider=1;

for(int i=0;i<(argLength-1);i++)
{
varDivider=varDivider*10;
}

return varDivider;
}

private long decimalTokenize(double decimalVal,int position)
{

long returnDecimalVal=0;
String strDecimalVal="";

if(decimalVal >0)
strDecimalVal = String.valueOf(decimalVal);

if(strDecimalVal.length()>0)
{
StringTokenizer decimalToken = new StringTokenizer(strDecimalVal,".");
//System.out.print("\n String tokenized successfully"+decimalToken.countTokens());
//int count = decimalToken.countTokens();

if(position==1)
{
returnDecimalVal = Long.parseLong(decimalToken.nextToken());
}
else if(position==2)
{
decimalToken.nextToken();
returnDecimalVal = Long.parseLong(decimalToken.nextToken());
}
}
return returnDecimalVal;
}

/**
*
* @param args
*/
public static void main(String[] args)
{
FindFileSize findFileSize = new FindFileSize();
}
}

Flat View: This topic has 3 replies on 1 page
Topic: how to write a driver for IntList.java Previous Topic   Next Topic Topic: IP Address Management

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use