Create, delete files and folders for Java file operations

Source: Internet
Author: User

This article explains the basic operations of Java files, including Java to create files and folders, Java delete files, Java to get all the files of the specified directory, Java to determine whether the specified path is a directory, and the Java search for the entire contents of the specified directory. Take a look at the example below.

Create a file
The two constants of File (File.separator, File.pathseparator). It is possible to use \ to split directly under Windows. But under Linux it's not. So, to make our code cross-platform, more robust, so, we all adopt these two constants.

 Public Static void Createfilet () {      file file=new file ("D:" +file.separator+ "IO" +file.separator+ "File01.txt "  );  //file File=new file ("D:\\io\\file01.txt"); You can use trycatch  (IOException e) {System.out.println ("IO exception") under Windows    );  E.printstacktrace (); }}  

Delete a file

 Public Static void    =new File ("D:" +file.separator+ "IO" +file.separator+ "File01.txt");   if(file.exists ()) {file.delete ();    }  Else{System.out.println ("file does not exist"); }}

Create a folder

 Public Static void    Createfilemix () {file File=new file ("D:" +file.separator+ "IO" +file.separator+ "file01");  File.mkdir (); }

Get all files (including hidden files) for the specified directory:

 Public Static void    getFile () {file File=new file ("D:" +file.separator);    String[]str=file.list ();      for (int i= 0; i< str.length; i++) {System.out.println (str[i]); }}

Determines whether the specified path is a directory

 Public Static void    filedirectory () {file File=new file ("D:" +file.separator+ "IO"); String str= File.isdirectory ()?    "Yes": "No";  System.out.println (str); }

Search all contents of a specified directory

 Public Static voidprint (file file) {if(file!=NULL) {if(File.isdirectory ()) {file [] Filearray=File.listfiles (); if(filearray!=NULL) { for(inti = 0; i < filearray.length; i++) {print (filearray[i]); }}}Else{System.out.println (file); }}}

Original address: http://www.manongjc.com/article/243.html

Related reading:

    1. Java writes a file using the Write () method
    2. Java readLine () Read file contents
    3. Java Delete () deleting files
    4. Java uses the read and write methods to copy the contents of a file to another file
    5. Java uses the FileWriter method to append data to a file
    6. Java uses the Createtempfile () method to create temporary files
    7. How Java modifies the last modified date of a file
    8. How Java gets the file size
    9. How to rename a file in Java
    10. How Java sets file Read-only
    11. Java uses the Exists () method to detect whether a file exists
    12. How Java creates files in the specified directory
    13. How Java gets file modification time
    14. Java Create file instance
    15. Java compare two file paths in the same directory

Create, delete files and folders for Java file operations

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.