zoukankan      html  css  js  c++  java
  • 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("文件夹删除失败");
                    }
                }
    
            }
    
    
        }
    }
  • 相关阅读:
    Pwn2Own 内核 TencentOS
    锤子思维的局限性
    内心宁静 Life
    ANTLRWorks: The ANTLR GUI Development Environment
    汇编 while vs for
    CatBoost is a high-performance open source library for gradient boosting on decision trees
    What is ERP
    专利 案件管理系统
    质量:零缺陷 & 零Bug
    SaaS协会 腾讯千帆
  • 原文地址:https://www.cnblogs.com/ligenyun/p/12641520.html
Copyright © 2011-2022 走看看