Solved

Get the size of a file

Posted on 2002-07-17
14
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

749 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