The usage of File in jieshenzhi Java

1.effect: a.Operation file  b.Operations folder c.Path to action file

2.Construction method
a.File(String pathname);Create a new pathname by converting the given pathname string to an abstract pathname File example.
b.File(String parent,String child);  according to parent Pathname strings and child Pathname string creates a new File example.
c.File(File parent,String chile);    according to parent Abstract pathnames and child Pathname string creates a new File example.
3.Method
a.Create function
public boolean createNewFile();    A new empty file is created indivisible when and only if there is no file with the name specified by this abstract pathname.
public boolean mkdir(); Create the directory specified by this abstract pathname.
public boolean mkdirs(); Create the directory specified by this abstract pathname, including all required but nonexistent parent directories.
b.Delete function
public boolean delete();  Delete the file or directory represented by this abstract pathname.
c.Judgment function
public boolean isDirectory()//Determine whether it is a folder
public boolean isFile();//Determine whether it is a document
public boolean exists();//Determine whether the file / folder exists
d.Get features
public String getPath()//Converts this abstract pathname to a pathname string.
public String getName();// Returns the name of the file or directory represented by this abstract pathname.
public long length();// Returns the length of the file represented by this abstract pathname.
public String getAbsolutePath();//  Returns the absolute pathname string for this abstract pathname.
public File getAbsoluteFile();//    Returns the absolute pathname form of this abstract pathname.
public File getParentFile();//  Returns the abstract pathname of the parent directory of this abstract pathname or null if no parent directory is specified for this pathname.
public String getParent(); Returns the pathname string of the parent directory of this abstract pathname, or null. 
public String [] list();//  Returns an array of strings specifying the files and directories in the directory represented by this abstract pathname.
public File [] listFiles();// Returns an array of abstract pathnames representing files in the directory represented by the abstract pathname.
4.Field summary (static member variable)
a.static String pathSeparator//  System related path separator, which is represented as a string for convenience.
b.static String separator;//  The default name separator associated with the system, which is represented as a string for convenience.
//Example:

 public class Demo01{
     public static void main(String [] args) throws IOException{

     }
     //Static member variable
     public static void fun1(){
         String pathseparator = File.pathSeparator;
         System.out.println(pathseparator);
         System.out.println(pathseparator);
/Construction method
//Relative path: with reference
//Absolute path: the path on the hard disk, which is unique
public static void fun3(){
//Here is the desktop / Test folder under Mac system
    File file = new File ("/Users/lanou/Desktop/Test");
    System.out.println(file);
    //Results / Users/lanou/Desktop/Test
 //Determine whether the path exists
 boolean b1 = file.exists();
 System.out.println(b1);
 //result:true
  //Use relative path, the reference is the current project root directory
  File file1 = new File("src/wl.txt");
  boolean b2 = file.exists();
  System.out.println(b2);
  //result:true

   //Get absolute path
  String absolutePath = file1.getAbsolutePath();
  System.out.println(absolutePath);
  //Results / users / lanou / eclipse workspace / sh-day023 / SRC / wl.txt

  //Use construction method 2
  String parent = "/Users/lanou/Desktop";
  String child = "/Test";
  //Parent and child paths
  File file2 = new File(parent,child);
  boolean b3 = file2.exists();
  System.out.println(b3);
  //result:true

  //Construction method 3
  File f = new File(parent);
  File file3 = new File (f,child);
  System.out.println(file3.exists());
  //result:true
}
//Create file - Method
public static void fun4(){

    File file = new File("/Users/lanou/Desktop/Test/haha.txt");
    boolean b1 = file.createNewFile();
    System.out.println(b1);
    Result: the file has not been created -- true, otherwise -- false
    File b2 = new File("src/dp.txt");
    boolean b = b2.createNewFile();
    System.out.println(b);
    Result: result: the file has not been created -- true, otherwise -- false
    Note: documents with or without suffix
}
 //Delete, deleted items, do not pass through the garbage collection bin, please be careful about the deleted items
 public  static void fun5(){
     File file = new File ("/Users/lanou/Desktop/Test/x");
     boolean b1 = file.delete();
     System.out.println(b1);
    Result: delete this file -- true, otherwise -- false
 }
//Judge whether the file exists
 public static void fun6(){
     File file = new File ("/Users/lanou/Desktop/Test/haha.txt");
     boolean b1 = file.isDirectory();
     System.out.println(b1);
   Result: there is this folder -- true, otherwise -- false
   boolean b2 = file.isFile();
   System.out.println(b2);
   Result: there is this file -- true, otherwise -- false

 }
//Get path, the path we get, whether it exists or not, will print the object path of the instance
 public static void fun7(){
     File file = new File("/Users/lanou/Desktop/Test/www.txt");
     Sytring path = file.getPath();
     System.out.println(path);
     Results / Users/lanou/Desktop/Test/www.txt
    //Get file name
    String name = file.getName();
    System.out.println(name);
    Result: www.txt
    //Get bytes occupied by file
    long length = file.length();
    System.out.println(length);
    Results: in this case, I input a "wang" in www.txt and print it as 4 bytes
    Note: English -- 1 byte
        Chinese -- 3 bytes (here UTF-8)
        The number of bytes in different encoding formats is different
 //Get parent path
   File parentFile = file.getParentFile();
   System.out.println(parentFile);
   Results / Users/lanou/Desktop/Test
 }
  //list and listFiles methods
  public void fun8(){
      File file = new File ("/Users/lanou/Desktop/Test");
      String [] list = file.list();
      for(String string : file.list){
          System.out..println(string);
        //result:
.DS_Store
g
haha
haha.txt
hahah.txt
www.txt
x
//Note: This is the name of all the files and folders under the Test folder I created, and only one layer can be viewed,.DE-Store It's a hidden file,

  File [] listFiles = file.listFiles();
  for(File file2 :listFiles){
      System.out.println(file2);
      //Result
/Users/lanou/Desktop/Test/.DS_Store
/Users/lanou/Desktop/Test/g
/Users/lanou/Desktop/Test/haha
/Users/lanou/Desktop/Test/haha.txt
/Users/lanou/Desktop/Test/hahah.txt
/Users/lanou/Desktop/Test/www.txt
/Users/lanou/Desktop/Test/x
//Note: listFiles returns the full path names of files and folders in the form of objects
        } 

      }
  //Traverse all files and folders under all folders under Test
 public static void fun9(){
     File file = new File("/Users/lanou/Desktop/Test");
    File [] listFiles = file.listFiles();
    for(File f : listFiles){
        if(f.isDirectory()){
            getFileName(f);
        }else{
            System.out.println(f);
//result:
/Users/lanou/Desktop/Test/.DS_Store
/Users/lanou/Desktop/Test/haha.txt
/Users/lanou/Desktop/Test/hahah.txt
/Users/lanou/Desktop/Test/www.txt
/Users/lanou/Desktop/Test/x/.DS_Store

/Users/lanou/Desktop/Test/x/y/z/www.txt
        }
    }
  }

}

Tags: Mac Eclipse encoding

Posted on Mon, 04 May 2020 08:00:43 -0700 by root