[Java iO] File Operations

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.