import java.io.*; public class FileDemo { public static void main(String[] args) { File f = new File("sample.txt"); System.out.println("File name: "+f.getName()); System.out.println("Path: "+f.getPath()); System.out.println("Absolute path: "+f.getAbsolutePath()); System.out.println("Parent: "+f.getParent()); System.out.println("Exists? "+f.exists()); System.out.println("Writable? "+f.canWrite()); System.out.println("Readable? "+f.canRead()); System.out.println("Directory? "+f.isDirectory()); System.out.println("File? "+f.isFile()); System.out.println("Last modified: "+f.lastModified()); System.out.println("Length: "+f.length()+" bytes"); //File r = new File("Newsample.txt"); //System.out.println("Renamed? "+f.renameTo(r)); /* the delete() method is used to delete a file or an empty directory*/ //f.delete(); // other methods System.out.println("Free space: "+f.getFreeSpace()); System.out.println("Total space: "+f.getTotalSpace()); System.out.println("Usable space: "+f.getUsableSpace()); System.out.println("Read-only: "+f.setReadOnly()); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter