java中实现判断文件是否存在,不存在则创建

内容摘要
一、判断文件是否存在,不存在则创建File file = new File("d: est.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
文章正文

一、判断文件是否存在,不存在则创建

1
2
3
4
5
6
7
8
9
10
11
File file = new File("d:    est.txt");
if (!file.exists()) {
    try {
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("文件已创建");
} else {
    System.out.println("文件已存在");
}

免费视频教程推荐:java免费视频教程

二、判断文件夹是否存在,不存在则创建

1
2
3
4
5
6
7
File folder = new File("d:  est1    est2");
if (!folder.exists() && !folder.isDirectory()) {
    folder.mkdirs();
    System.out.println("创建文件夹");
} else {
    System.out.println("文件夹已存在");
}

更多相关文章教程推荐:java语言入门

代码注释
[!--zhushi--]

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!