Directory:
1. Move a file/rename
2. delete an object
3. List Files in the directory
1. Move a file/rename
Package mjn. io; import Java. io. file;/*** file operation: Move the file/rename * @ author mjn * @ date 2011-10-03 */public class filerename {public static void main (string [] ARGs) {file = new file ("F:/howtobeaprogrammer.pdf"); // the original file is moved to the F:/test directory and renamed 'test10000' Boolean success = file. renameto (new file ("F:/test/testeto"); system. out. println ("Move/rename Result:" + success );}}
2. delete an object
/*** File operation: delete the file * @ author mjn * @ date 2011-10-03 */public class filedelete {public static void main (string [] ARGs) {file = new file ("F:/test/testess"); Boolean success = file. delete (); system. out. println ("Move/rename Result:" + success );}}
3. List Files in the directory
Package mjn. io; import Java. io. file;/*** file operation: List objects in the directory * @ author mjn * @ date 2011-10-03 */public class filelist {public static void main (string [] ARGs) {file = new file ("C:/"); file [] files = file. listfiles (); For (file F: Files) {system. out. println (f );}}}
Program running result:
c:\AUTOEXEC.BATc:\bootc:\boot.inic:\bootfont.binc:\CONFIG.SYSc:\Documents and Settingsc:\driversc:\GRLDRc:\IO.SYSc:\log_config.datc:\menu.lstc:\MSDOS.SYSc:\MSOCachec:\NTDETECT.COMc:\ntldrc:\pagefile.sysc:\Program Filesc:\RECYCLERc:\System Volume Informationc:\WINDOWS
If you change the code in the for loop in the code:
System.out.println(f.getName());
Only the file name is output. There is another method for the output file name:
File file = new File("c:/"); String[] fileNames = file.list(); for (String fileName : fileNames) { System.out.println(fileName); }
References:
Http://tutorials.jenkov.com/java-io/file.html