Java 简明教程

Java - Directory Operations

Directory in Java

目录是一个文件,它可以包含其他文件和目录列表。可以使用 File 对象来创建目录、列出目录中可用的文件。有关完整详细信息,请查看可对文件对象调用的所有方法的列表,以及与目录相关的内容。

A directory is a File which can contain a list of other files and directories. You use File object to create directories, to list down files available in a directory. For complete detail, check a list of all the methods which you can call on File object and what are related to directories.

Creating Directories

有两个有用的 File 实用方法可用于创建目录 −

There are two useful File utility methods, which can be used to create directories −

  1. The mkdir() method creates a directory, returning true on success and false on failure. Failure indicates that the path specified in the File object already exists, or that the directory cannot be created because the entire path does not exist yet.

  2. The mkdirs() method creates both a directory and all the parents of the directory.

Example to Create Directory in Java

以下示例创建 "/tmp/user/java/bin" 目录 −

Following example creates "/tmp/user/java/bin" directory −

package com.tutorialspoint;

import java.io.File;

public class DirectoryTest {
   public static void main(String args[]) {
      String dirname = "/tmp/user/java/bin";
      File directory = new File(dirname);

      // Create directory now.
      directory.mkdirs();

      // create new file object
      File file = new File("/tmp/user/java/bin");

      System.out.println(file.exists());
   }
}

编译并执行以上代码,创建“/tmp/user/java/bin”文件夹。

Compile and execute the above code to create "/tmp/user/java/bin" folders.

true

Note − Java 会自动按照惯例处理 UNIX 和 Windows 上的路径分隔符。如果你在 Java 的 Windows 版本上使用正斜杠 (/),路径仍然会正确解析。

Note − Java automatically takes care of path separators on UNIX and Windows as per conventions. If you use a forward slash (/) on a Windows version of Java, the path will still resolve correctly.

Listing (Reading) Directories

可以使用 File 对象提供的 list() method 按以下方式列出目录中可用的所有文件和目录 -

You can use list() method provided by File object to list down all the files and directories available in a directory as follows −

Example to Read (List) a Directory in Java

package com.tutorialspoint;

import java.io.File;

public class DirectoryTest {

   public static void main(String[] args) {
      File file = null;
      String[] paths;

      try {
         // create new file object
         file = new File("/tmp");

         // array of files and directory
         paths = file.list();

         // for each name in the path array
         for(String path:paths) {
            // prints filename and directory name
            System.out.println(path);
         }
      } catch (Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

这将根据 /tmp 目录中可用的目录和文件生成以下结果 −

This will produce the following result based on the directories and files available in your /tmp directory −

user

Deleting Directories

可以使用 File 对象提供的 delete() method 按以下方式删除目录 -

You can use delete() method provided by File object to delete a directory as follows −

Example to Delete a Directory in Java

package com.tutorialspoint;

import java.io.File;

public class DirectoryTest {

   public static void main(String[] args) {
      File file = new File("/tmp/user/java/bin");
      if(file.exists()) {
         boolean success = file.delete();

         if (success) {
            System.out.println("The directory has been successfully deleted.");
         }else {
            System.out.println("The directory deletion failed.");
         }
      }else {
         System.out.println("The directory is not present.");
      }
   }
}

这将根据 /tmp 目录中可用的目录和文件生成以下结果 −

This will produce the following result based on the directories and files available in your /tmp directory −

The directory has been successfully deleted.