• java File文件操作


    package com.jack.file;
    
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Path;
    
    public class main {
        public static void main(String[] args) throws Exception {
            System.out.println(File.pathSeparator);//:
            System.out.println(File.separator);//   
            System.out.println(File.pathSeparatorChar);//  :
            System.out.println(File.separatorChar); //  
    
             
            File file = new File("D:\test");
            if (!file.exists()) {
                if (!file.isDirectory()) {
                    if (file.createNewFile()) {
                        System.out.println("文件创建了");
                    } else {
                        System.out.println("文件创建失败了");
                    }
                } else {
                    if (file.mkdir()) {
                        System.out.println("文件夹创建了");
                    } else {
                        System.out.println("文件夹创建失败了");
                    }
                }
            }
    
            if (file.exists()) {
                System.out.println(file);
                System.out.println(file.getName());
                System.out.println(file.getPath());
    
                if (file.isDirectory()) {
                    System.out.println("这是目录");
                    String[] child = file.list();
                    for (int i = 0; i < child.length; i++) {
                        System.out.println(child[i]);
                    }
                }
                if (!file.isDirectory()) {
                    if (file.delete()) {
                        System.out.println("文件删除了");
                    } else {
                        System.out.println("文件删除失败");
                    }
                } else {
                    //删除目录,必须目录为空的时候,file.delete才可用
                    //递归,删除文件夹下面的文件,再删除文件夹
                    if (file.delete()) {
                        System.out.println("文件夹删除了");
                    } else {
                        System.out.println("文件夹删除失败");
                    }
                }
    
            }
    
    
        }
    }
  • 相关阅读:
    个人博客12
    《梦断代码》阅读笔记03
    个人博客11
    个人博客10
    【Codeforces 404C】Restore Graph
    【Codeforces 476C】Dreamoon and Sums
    【Codeforces 242C】King's Path
    【Codeforces 382C】Arithmetic Progression
    【Codeforces 1096D】Easy Problem
    【Codeforces 494A】Treasure
  • 原文地址:https://www.cnblogs.com/ligenyun/p/12641520.html
Copyright © 2020-2023  润新知