Java 创建临时文件或目录

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

Java 创建临时文件示例

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

public class CreateTempFileExample {
    public static void main(String[] args) {
        try {
            Path tempFilePath = Files.createTempFile("sampleTempFile", ".txt");
            System.out.printf("Temp file created at %s%n", tempFilePath);
        } catch (IOException ex) {
            System.out.format("I/O error: %s%n", ex);
        }
    }
}

Java 创建临时目录示例

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

public class CreateTempDirectoryExample {
    public static void main(String[] args) {
        try {
            Path tempDirPath = Files.createTempDirectory("tempDir");
            System.out.printf("Temporary folder created at %s%n", tempDirPath);
        } catch (IOException ex) {
            System.out.format("I/O error: %s%n", ex);
        }
    }
}

相关文章

微信公众号

最新文章

更多