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());
}
}
}