Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get the size of a file

Posted on 2002-07-17
14
Medium Priority
?
244 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Accepted Solution

by:
Fortress_Initiative earned 200 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses

618 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