File class
/*** Path delimiter: public static final String separator; * Set file path: Public files (String pathName); * Create file: public boolean creatnewfile () throws IOException; * Determine if the file exists: public boolean exists (); * Delete File: public boolean delete (); * Find Parent path: public File getpareentfile (); * Create Parent path: public boolean mkdir (); * Create multi-level path: public boolean mkdirs (); */ PackageCom.java.demo;ImportJava.io.File; Public classTestdemo { Public Static voidMain (String args[])throwsexception{File FL=NewFile ("E:" +file.separator + "Hello" +file.separator+ "demo" +file.separator+ "Java.txt");//set the full path of the file if(!fl.getparentfile (). exists ()) {//if the parent path does not existFl.getparentfile (). Mkdirs ();//Create parent Path } if(Fl.exists ()) {//determine if a file existsFl.delete ();//If the file exists, delete the file;}Else{fl.createnewfile (); //do not exist then create file } } }
/*** Get file Size: public long length (); * Determine if File: Public boolean isfile (); * Determine if directory: public boolean isdirectory (); * Last modified time : Public long lastmodified (); * Get file Full path: public String getpath (); */ PackageCom.java.demo;ImportJava.io.File;ImportJava.math.BigDecimal;ImportJava.text.SimpleDateFormat;Importjava.util.Date; Public classTestdemo { Public Static voidMain (String args[])throwsexception{File FL=NewFile ("E:" +file.separator + "Msdia80.dll");//set the full path of the fileSystem.out.println (Fl.getpath ());//E:\msdia80.dllSystem.out.println ("is file" +fl.isfile ()); System.out.println ("Is directory" +fl.isdirectory ()); if(Fl.exists ()) {System.out.println ("Size of File:" + (NewBigDecimal ((Double) Fl.length ()/1024/1024). Divide (NewBigDecimal (1), 2,bigdecimal.round_half_up). Doublevalue ()) + "M"); } System.out.println ("Last Modified Time" +NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss. SSS "). Format (NewDate (Fl.lastmodified ())); } }
/*** 1. List the information in the directory: public String [] list (); * 2. Return directory information through the File object: public file[] Listfiles ();*/ PackageCom.java.demo;ImportJava.io.File;ImportJava.math.BigDecimal;ImportJava.text.SimpleDateFormat;Importjava.util.Date; Public classTestdemo { Public Static voidMain (String args[])throwsexception{File FL=NewFile ("E:" +file.separator);//set the full path of the file if(Fl.isdirectory ()) {String result[]=fl.list (); for(intx=0;x<result.length;x++) {System.out.println (result[x]);//$RECYCLE. BIN}} File [] Reslut=Fl.listfiles (); for(inty=0;y<reslut.length;y++) {System.out.println (reslut[y]);//e:\ $RECYCLE. BIN } } }
IO operations in Java