import Java.io.*; class FileConstructors { public static void main(String args[]) { try { File F1 = new File("PDIR/CDIR"); File F2 = new File("PDIR/CDIR","data/file.txt"); File F3 = new File(F1,"data/file2.txt"); } catch (IOException ex) { ex.printStackTrace(); } } }Explanation:
Output: F1: Pathname = C:/JDK/Bin/PDIR/CDIR F2: Pathname = C:/JDK/Bin/PDIR/CDIR/data/file1.txt F3: Pathname = C:/JDK/Bin/PDIR/CDIR/data/file2.txt
Methods | Description |
---|---|
boolean createNewFile() | Creates new file, if the file name at specified path doesn't exist |
boolean exists() | It returns true, if file or directory is associated with file instance exist, else returns true. |
boolean delete() | Deletes the file or directory represented by the file instance. |
boolean isDirectory () | Returns true, if the file instance represents a directory |
boolean isFile() | Returns true, if the file instance represents a file. |
String[] list() | Returns the list of files and directories contained by a directory represented by the File instance. |
File[] listFiles() | Returns the list of files & directories contained in the directory represented by this File instance. |
boolean mkdir() | Creates a directory at specified path. |
boolean renameTo(File object) | Renames the file or directory represented by this File instance to new name specified in new File instance. |
boolean canRead() | Returns true if the file or directory represented by file instance is readable by program, else returns false. |
boolean canWrite() | Returns true if the File directory associated with File instance can be modified |
String getName() | Returns name of file and directory represented by File instance. |
//FileClass_Fx_3.java //Program to display list of files and directories import java.io.File; public class FileClass_Fx_3 { public static void main(String []args) { File ff=new File("c:\\windows"); int fcount=0,dcount=0; File []listFile=ff.listFile(); for(File s: listFile) { if(s.isFile()) { System.out.println("File Name="+s); fcount++; } else { System.out.println(">Dir Name="+s); dcount++; } } System.out.println("File="+fcount+"& Directories="+dcount); } }
Output Dir Name=c:\windows\addins > Dir Name=c:\windows\AppCompat > Dir Name=c:\windows\AppPatch > Dir Name=c:\windows\assembly File Name=c:\windows\atiogl.xml File Name=c:\windows\ativpsrm.bin ….. ….. Files=41 & Directories=58
//FileClass_Fx_2.java //Program to delete a file import java.io.File; public class FileClassFx_2 { public static void main(String []args) { File delFile=new File("c:\\chk4.java"); boolean b=delFile.delete(); if(b==true) System.out.Println("File deleted"); else System.out.println("File not Found"); } }
Output File deleted
//FileClass_Fx_1.java //Program using various methods of File class import java.io.*; public class FileClassFx_1 { public static void main(String[] args) { try { File fff=new File("c://chk.java"); System.out.println("Is it Exist:"+fff.exist()); boolean b=fff.createNewFile(); if(b==true) System.out.println("File Created"); else System.out.println("File ALready Exist"); System.out.println("Is it Exist Now:"+fff.exist()+"|"); System.out.println("Parent="+fff.getParent()+"|"); System.out.println("Parent="+fff.isDirectory()+"|"); System.out.println("isFile="+fff.isFile()); } catch(Exception e) { e.printStackTrace(); } } }
Output: Is it Exist:false File Created... Is it Exist Now : true | Parent=c:\ | isDir=false| is File=true
//FileClass_Fx_4.java //Program to create a directory and file' import java.io.*; public class FileClass_Fx_4{ public static void main(String[] args) { try { File nwDir=new File("c:\\creator"); boolean b=nwDir.mkdir(); if(b==true) System.out.println("Directory Created ..."); else System.out.println("File Already Exist..."); } catch(IOException ex) { ex.printStackTrace(); } } }
Output Directory Created... File Created....