1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package io; import java.io.File; import java.io.IOException; public class FileOrDir { public static void main(String[] args) { File f = new File("D:\\first.txt"); File f1 = new File("D:\\second.txt"); if (f.exists() == false) { System.out.println("File or Directory does not exists"); } if (f.isDirectory()) { System.out.println("This is a directory"); } if (f.isFile()) { System.out.println("This is a file"); System.out.println("File name :" + f.getName()); System.out.println("File path :" + f.getPath()); System.out.println("File Absolute path :" + f.getAbsolutePath()); try { System.out.println("File Canonical path :" + f.getCanonicalPath()); } catch (IOException ex) { System.out.println("IOException " + ex); } System.out.println("File Parent :" + f.getParent()); System.out.println("File can execute :" + f.canExecute()); System.out.println("File can read :" + f.canRead()); System.out.println("File can write :" + f.canWrite()); System.out.println("File last Modified :" + f.lastModified()); try { if (f1.createNewFile()) { System.out.println("File created "); } else { if (f1.exists()) { System.out.println("File already exists "); f1.delete(); System.out.println("File is get deleted "); if (f1.createNewFile()) { System.out.println("now File created "); } } } } catch (IOException ex) { System.out.println("IOException " + ex); } System.out.println("Setting the file readable status :" + f1.setReadable(true)); System.out.println("Setting the file writable status :" + f1.setWritable(false)); System.out.println("Can we write on file :" + f1.canWrite()); } } } |