Report a file's status
import java.io.File;
import java.io.IOException;
import java.util.Date;
public class Main {
public static void main(String[] argv) throws IOException {
status(".");
}
public static void status(String fileName) throws IOException {
System.out.println("FileName:" + fileName);
File f = new File(fileName);
if (!f.exists()) {
System.out.println("file not found");
System.out.println(); // Blank line
return;
}
System.out.println("Canonical name " + f.getCanonicalPath());
String p = f.getParent();
if (p != null) {
System.out.println("Parent directory: " + p);
}
if (f.canRead()) {
System.out.println("File is readable.");
}
if (f.canWrite()) {
System.out.println("File is writable.");
}
Date d = new Date();
d.setTime(f.lastModified());
System.out.println("Last modified " + d);
if (f.isFile()) {
System.out.println("File size is " + f.length() + " bytes.");
} else if (f.isDirectory()) {
System.out.println("It's a directory");
} else {
System.out.println("I dunno! Neither a file nor a directory!");
}
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
IO File:
- Compare File Dates
- Compress files using with ZIP
- Concatenate files
- Copy a File with NIO FileChannel and ByteBuffer
- Copy a file with FileReader and FileWriter
- Copy a file with InputStream and OutputStream
- Copy a file and overwrite
- Delete a file
- Delete File Recursively
- Get readable file size
- Move a file
- Rename a file
- Report a file's status
- Search a file by regular expressions
- Touch a file: set File Last Modified Time