Solved

Get the size of a file

Posted on 2002-07-17
14
206 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
>>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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Expert Comment

by:vemul
Comment Utility
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
Comment Utility
Sounds good to me :-)
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
hehe ;)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
What's happening here vemul?
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
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
Comment Utility
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
Comment Utility
OK
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
SPLUNK REST  API call to Splunk to create and index? 2 67
Receive file in Servlet 1 36
Java and GPO 11 46
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)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

743 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

17 Experts available now in Live!

Get 1:1 Help Now