Java File class usage details __ "03" Java

Source: Internet
Author: User

Java file classes are very powerful, using Java to basically do all the work on a file. This article will analyze the Java file Operation class in detail, and briefly introduce the common methods of file classes, which Java developers need to look at. Constructors

Public  class  Filedemo {
      public  static  void  Main (string [] args) {
          //constructor file (string Pathname) 
         f1 = new file ("c:\\ ABC \ 1.txt");
         File (String parent,string child) 
         F2 = new file ("c:\\ abc", "2.txt");
         The file (file parent,string child) 
         Files F3 = new File ("C:" + File.separator + "abc");
         System. Out. println (F1); C:\abc\1.txt

     }

 }
Create Method

1.boolean CreateNewFile () does not exist return true exists return false
2.boolean mkdir () Create directory
3.boolean mkdirs () Create a multilevel directory deletion method

1.boolean Delete ()
2.boolean deleteonexit () file is deleted after use is complete

Import Java.io.File;
Import java.io.IOException;
Public  class  FileDemo2  {
     public  static void Main (String [] args) {
         file F = new File ("D:\\ 1.txt ");
        try {
             System. Out. println (F.createnewfile ()); Returns false System. Out when a file exists 
            . println (F.delete ()); Returns False when a file does not exist 
        } catch (IOException e) {
             //TODO automatically generated catch block
            e.printstacktrace ();
}}}
Judging Method

1.boolean CanExecute () to determine whether the file is executable
2.boolean CanRead () to determine whether the file is readable
3.boolean CanWrite () to determine whether a file is writable
4.boolean exists ()
Determine if the file exists 5.boolean isdirectory ()
6. Boolean isfile ()
7.boolean Ishidden ()
8.boolean Isabsolute () determines whether the absolute path file does not exist and can determine the method of obtaining

1.String GetName ()
2.String GetPath ()
3.String GetAbsolutePath ()
4.String getparent ()//If no parent directory returns null
5.long lastmodified ()//To get the last modification time
6. Length ()
7.boolean Renameto (File f)
8.File [] liseroots ()//Get Machine Letter
9.String [] List ()
10.String [] List (FilenameFilter filter)
list files and folders under the disk

Public  class  FileDemo3 {
      public  static  void  main (String [] args) {
         File [] files = File.listroots ();
         For (file: file) {
             system. Out. println (file);
             if (file.length () > 0) {
                 String [] filenames = File.list ();
                 for (String filename:filenames) {
                     system. Out. println (filename);}}}}

 
File Filtering
Import Java.io.File;
 Import Java.io.FilenameFilter;
 Public  class  FileDemo4  {
      public  static  void  main (String [] args) {
         File [] files = File.listroots ();
         For (file: file) {
             System.out.println (file);
             if (file.length () > 0) {
                 String [] filenames = file.list (new FilenameFilter () {
                      //File filter directory name file name public  Boolean  Accept (File file,string filename) {return
                          filename.endswith (". mp3");
                     }
                 );
                 for (String filename:filenames) {
                     System.out.println (filename);
                 }}
         }
     }

 

File [] Listfiles ()

file [] Listfiles (filenamefilter filter) lists all files recursively by recursion

Public  class  FileDemo5 {
     public  static  void  main (String [] args) {
        file F = new file ("e:\\ Music ");
        Showdir (F);
    }
    Public  static  void  Showdir (File dir) {
        system. Out. println (dir);
        File [] files = Dir.listfiles ();
        For (file: file) {
             if (file.isdirectory ())
                showdir (file);
            Other  
                systems. Out. println (file);}}}
Moving Files

Locate all the. java files under D disk, copy them to the C:\ Jad directory, and change the type of all files from. java to. jad.

public class Test5 {public static void main (String [] args) {File f1 = new File ("d:\\");
    MoveFile (F1);
    public static void MoveFile (file dir) {file [] files = Dir.listfiles ();
        For (file: file) {if (File.isdirectory ()) moveFile (file); else {if (File.getname (). EndsWith (". Java")) File.renameto (New file ("c:\\ jad \" + file.getname (). SUBSTRING (0,file.getname (). LastIndexOf ('.
            ') + ". Jad")); }
        }
    }
}

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.