java 创建文件夹和创建文件

方法和结果

 

 说明:

1文件夹

  if(file.getParentFile().exists()) {//判断文件夹是否存在
            System.out.println("文件夹存在!!");
        }else {
            System.out.println("文件夹不存在!!");    
            file.getParentFile().mkdirs();//mkdir();创建一级目录
        }

2文件

  if (file.exists()) {//判断文件是否存在
            System.out.println("存在!!");
        }else {
            System.out.println("不存在!!");    
            try {
                file.createNewFile();
            } catch (IOException e) {
                System.out.println("文件创建失败");
                e.printStackTrace();
            }
        }

3判断file属性 

扫描二维码关注公众号,回复: 9276032 查看本文章

  isDirectory()是检查一个对象是否是文件夹。

@GF

猜你喜欢

转载自www.cnblogs.com/gwf2020/p/12334264.html