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
Solved

Get the size of a file

Posted on 2002-07-17
14
228 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

808 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