nywebsol
asked on
Get the size of a file
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?
What I need to do is get the size of each file...
any idea?
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.getNa me() + " size: " + f.length() + " bytes");
}
}
}
import java.io.*;
public class FileSizes {
public static void main(String[] args){
if(args.length < 1){
System.out.println("Usage:
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
}
}
else {
System.out.println(f.getNa
}
}
}
And while we're about it, import java.io.*; is sloppy!
Better import java.io.File;
Better import java.io.File;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...
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...
>>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.
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.
>>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
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
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
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
Sounds good to me :-)
hehe ;)
What's happening here vemul?
WTF?
That was the wrong answer to accept in my opinion...
I guess nywebsol just went for the quick approach, not the correct one :-(
That was the wrong answer to accept in my opinion...
I guess nywebsol just went for the quick approach, not the correct one :-(
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
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
OK
public class FileSizes {
public static void main(String[] args){
if(args.length < 1){
System.out.println("Usage:
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
}
}
else {
System.out.println(f.getNa
}
}
}