Solved

Get the size of a file

Posted on 2002-07-17
14
215 Views
Last Modified: 2010-03-31
I currently am searching a directory and writing out to a text file all the files in the directory.
What I need to do is get the size of each file...
any idea?
0
Comment
Question by:nywebsol
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 7160626
import java.io.*;

public class FileSizes {

  public static void main(String[] args){
    if(args.length < 1){
     System.out.println("Usage: <file/directory to list>");
     System.exit(-1);
    }
    File f = new File(args[0]);
    if(f.isDirectory()){
      File[] files = f.listFiles();
      for(int i= 0;i < files.length;i++){
        System.out.println(files[i].getName() + " size: " + files[i].length() + " bytes");

      }
    }
    else {
      System.out.println(f.getName() + " size: " + f.length() + " bytes");
    }
  }

}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7160631
Sorry - slight error there. SHould have been:

import java.io.*;

public class FileSizes {

  public static void main(String[] args){
    if(args.length < 1){
     System.out.println("Usage: java FileSizes <file/directory to list>");
     System.exit(-1);
    }
    File f = new File(args[0]);
    if(f.isDirectory()){
      File[] files = f.listFiles();
      for(int i= 0;i < files.length;i++){
        System.out.println(files[i].getName() + " size: " + files[i].length() + " bytes");
      }
    }
    else {
      System.out.println(f.getName() + " size: " + f.length() + " bytes");
    }
  }

}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7160635
And while we're about it, import java.io.*; is sloppy!
Better import java.io.File;
0
 

Accepted Solution

by:
Fortress_Initiative earned 50 total points
ID: 7161787
import java.io.*;

//~Code snippet~//

try {
  File file = new File(strFilename);
  //See if the file is there, optional
  if(!file.exists()) {
  //report the error
  }

  FileInputStream fis = new FileInputStream(file);
  int iFileSize = fis.available();
  fis.close();
} catch (........
//~Code snippet~//
0
 
LVL 35

Expert Comment

by:TimYates
ID: 7687070
Why is it sloppy?

How is the generated code different?

Or are you just being pedantic, in that someone might have defined another Reader class in the current project, and so the compiler might pick the wrong one?

Just wondering...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7687154
>>Why is it sloppy?

Because it doesn't show to readers of the source exactly which classes are being imported. I wouldn't take this principle to an extreme though.

Incidentally, does TimYates == nywebsol? If so, i'd be glad if you could close the question. EE has had to employ extra staff due to the large number of open questions.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 7688241
>>Because it doesn't show to readers of the source exactly which classes are being imported. I wouldn't take this principle to an extreme though.

Ahhhh... K :-)

>>Incidentally, does TimYates == nywebsol? If so, i'd be glad if you could close the question. EE has had to employ extra staff due to the large number of open questions.

Nope...soz :-(

assert( TimYates==TimYates ) ;

:-D

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:vemul
ID: 7887887
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- points to CEHJ

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7890806
Sounds good to me :-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 7891058
hehe ;)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7891815
What's happening here vemul?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 7892075
WTF?

That was the wrong answer to accept in my opinion...

I guess nywebsol just went for the quick approach, not the correct one :-(
0
 
LVL 5

Expert Comment

by:vemul
ID: 7892604
CEHJ,
As a clean up volunteer, I volunteered to clean up this section and in my opinion, your answer was correct and that is what I had recommended but I have no say in who the requester(ie nywebsol) finally decides to give the points to.

vemul
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7892615
OK
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now