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("文件夹删除失败");
                    }
                }
    
            }
    
    
        }
    }
  • 相关阅读:
    面向对象-类与实例化对象
    面向对象-面向对象与面向过程优缺点
    pyCharn文件模板变量
    安装pycrypto以及报错处理
    文件-读、写、追加已经文件操作
    文件-读取与编码检测
    运算-三元运算
    三方工具-sqlacodegen
    函数-生成器之斐波拉契数列
    python内存-fromkeys
  • 原文地址:https://www.cnblogs.com/ligenyun/p/12641520.html
Copyright © 2011-2022 走看看