Things could be very simple in Java !!!!!
So many times, I have seen that there are many ways of achieving a result in java. Most often, we tend to use clasess/methods provided by JDK which is infact a good practise but sometimes, it is actually quite simpler without using them.
Hey, what if we have a task of developing a small java program to list the names of all files in a particular directory.
It looks like the simplest way of achieveing this would be :
Using File.list() or File.listFiles() after confirming that current file is a directory.
However, there could be another simpler way which does not use utility methods of File class:
public class dirList {
public static void main(String args[]) {
for (int i=0; i<args.length;i++) {
System.out.println(args[i]);
}
}
}
Now, compile and run this program dirList from the particualr directory whose listing is to be done. The argument to be passed is asterisk (*) .. i.e.
java dirList *
This would give us desired result. Actually, * in DOS environment means all the contents of current directory, which is transferred to java program as argument and program just prints them.
This program has a limitation that it should be executed from that particular directory but the point is that it satisfies the requirement.
Note: This is tested on windows environment only.
[This content was published on my official blog on 4/30/2010 1:41 AM]
Hey, what if we have a task of developing a small java program to list the names of all files in a particular directory.
It looks like the simplest way of achieveing this would be :
Using File.list() or File.listFiles() after confirming that current file is a directory.
However, there could be another simpler way which does not use utility methods of File class:
public class dirList {
public static void main(String args[]) {
for (int i=0; i<args.length;i++) {
System.out.println(args[i]);
}
}
}
Now, compile and run this program dirList from the particualr directory whose listing is to be done. The argument to be passed is asterisk (*) .. i.e.
java dirList *
This would give us desired result. Actually, * in DOS environment means all the contents of current directory, which is transferred to java program as argument and program just prints them.
This program has a limitation that it should be executed from that particular directory but the point is that it satisfies the requirement.
Note: This is tested on windows environment only.
[This content was published on my official blog on 4/30/2010 1:41 AM]
Comments
Post a Comment