Skip to content

Instantly share code, notes, and snippets.

@ardeshir
Created November 5, 2013 20:06
Show Gist options
  • Save ardeshir/7325275 to your computer and use it in GitHub Desktop.
Save ardeshir/7325275 to your computer and use it in GitHub Desktop.
Get a file from the cmd line and walk the file system.
import java.io.*;
import java.util.*;
public class FileWalker {
public static void main(String[] args) {
for( int i = 0; i < args.length; i++ ) {
File f = new File(args[i]);
if(f.exists()) {
System.out.println("Name: " + f.getName());
System.out.println("Absolute path: " + f.getAbsolutePath() );
try {
System.out.println("Canonical path: " + f.getCanonicalPath() );
} catch (IOException ex) {
System.out.println("Couldn't determine canonical path.");
}
// add code for file desriptions here:
String parent = f.getParent();
if(parent != null) {
System.out.println("Parent: " + f.getParent());
}
if(f.canWrite()) System.out.println(f.getName() + " is writable");
if(f.canRead()) System.out.println(f.getName() + " is readable");
if(f.isFile()) {
System.out.println(f.getName() + " is a file");
} else if (f.isDirectory()) {
System.out.println(f.getName() + " is a directory");
} else {
System.out.println("What is this?!");
}
if(f.isAbsolute()) {
System.out.println(f.getPath() + " is an absolute path.");
} else {
System.out.println(f.getPath() + " is not an absolute path.");
}
long lm = f.lastModified();
if(lm != 0) System.out.println("Last modified at " + new Date(lm));
long length = f.length();
if (length != 0) {
System.out.println(f.getName() + " is " + length + " bytes long.");
}
} else {
System.out.println("I'm sorry. I can't find the file " + args[i]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment