如何在 Java 中创建一个新文件

x33g5p2x  于2021-10-16 转载在 Java  
字(3.1k)|赞(0)|评价(0)|浏览(406)

您可以通过多种方式在 Java 中创建新文件。在本文中,我概述了两种最推荐的创建新文件的方法。

使用 Java NIO 创建新文件(推荐)- JDK 7+

您可以使用 Files.createFile(path) 方法在 Java 中创建一个新文件:

import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CreateNewFile {

    public static void main(String[] args)  {
        // New file path
        Path filePath = Paths.get("./bar.txt");

        try {
            // Create a file at the specified file path
            Files.createFile(filePath);
            System.out.println("File created successfully!");

        } catch (FileAlreadyExistsException e) {
            System.out.println("File already exists");
        } catch (IOException e) {
            System.out.println("An I/O error occurred: " + e.getMessage());
        } catch (SecurityException e) {
            System.out.println("No permission to create file: " + e.getMessage());
        }
    }
}

使用 Java NIO 创建缺少父目录的新文件

在某些情况下,您可能希望在创建文件时创建任何丢失的父目录。您可以使用 Files.createDirectories(path) 函数在创建文件之前创建缺少的父目录。

import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CreateNewFile {

    public static void main(String[] args)  {
        // New file path
        Path filePath = Paths.get("java/io/bar.txt");

        try {
            // Create missing parent directories
            if(filePath.getParent() != null) {
                Files.createDirectories(filePath.getParent());
            }

            // Create a file at the specified file path
            Files.createFile(filePath);
            System.out.println("File created successfully!");

        } catch (FileAlreadyExistsException e) {
            System.out.println("File already exists");
        } catch (IOException e) {
            System.out.println("An I/O error occurred: " + e.getMessage());
        } catch (SecurityException e) {
            System.out.println("No permission to create file: " + e.getMessage());
        }
    }
}

使用 java.io.File 类在 Java 中创建新文件 - JDK 6+

您还可以使用 File.createNewFile() 方法在 Java 中创建一个新文件。它返回一个布尔值,它是 -

  • true, 如果文件不存在并且创建成功
  • false,如果文件已经存在
import java.io.File;
import java.io.IOException;

public class CreateNewFile {

    public static void main(String[] args) {
        // Instantiate a File object with a file path
        File file = new File("./foo.txt");

        try {
            // Create the file in the filesystem
            boolean success = file.createNewFile();

            if (success) {
                System.out.println("File created successfully!");
            } else {
                System.out.println("File already exists!");
            }
        } catch (IOException e) {
            System.out.println("An I/O error occurred: " + e.getMessage());
        } catch (SecurityException e) {
            System.out.println("No sufficient permission to create file: " + e.getMessage());
        }
    }
}

使用 java.io.File 类创建新文件以及缺少的父目录

如果要在创建文件时创建缺少的父目录,则可以通过调用 file.getParentFile().mkdirs() 方法显式创建目录:

import java.io.File;
import java.io.IOException;

public class CreateNewFile {

    public static void main(String[] args) {
        // Instantiate a File object with a file path
        File file = new File("java/io/foo.txt");

        try {
            // Create missing parent directories
            if(file.getParentFile() != null) {
                file.getParentFile().mkdirs();
            }

            // Create the file
            boolean success = file.createNewFile();

            if (success) {
                System.out.println("File created successfully!");
            } else {
                System.out.println("File already exists!");
            }
        } catch (IOException e) {
            System.out.println("An I/O error occurred: " + e.getMessage());
        } catch (SecurityException e) {
            System.out.println("No sufficient permission to create file: " + e.getMessage());
        }
    }
}

相关文章