• File类操作文件


    简单示例:

     1     public static void main(String[] args) {
     2         // 列出系统所有的根路径
     3         File[] listRoots = File.listRoots();
     4         for (File file : listRoots) {
     5             // 相对路径
     6             System.out.println(file.getPath());
     7 
     8             System.out.println("空闲未使用 = " + file.getFreeSpace() / 1024 / 1024 / 1024 + "G");// 空闲空间
     9             System.out.println("可使用 = " + file.getUsableSpace() / 1024 / 1024 / 1024 + "G");// 可用空间
    10             System.out.println("已经使用 = " + (file.getTotalSpace() - file.getUsableSpace()) / 1024 / 1024 / 1024 + "G");// 已使用用空间
    11             System.out.println("总容量 = " + file.getTotalSpace() / 1024 / 1024 / 1024 + "G");// 总空间
    12             // 绝对路径
    13             System.out.println(file.getAbsolutePath());
    14             System.out.println();
    15             File file2 = new File(file.getPath().charAt(0) + ":\dick\text1.txt");
    16             System.out.println(file2.getPath());
    17             System.out.println(file2.exists());
    18             //one way
    19             /*if (!file2.exists()) {
    20                 try {
    21                     // 创建父级目录
    22                     boolean mkdirs = file2.getParentFile().mkdirs();
    23                     System.out.println("创建" + mkdirs);
    24                     file2.createNewFile();
    25                     
    26                     //new File("D://a//b//rjl.txt").createNewFile();创建失败,需要先创建目录,再创建文件
    27                 } catch (IOException e) {
    28                     // TODO Auto-generated catch block
    29                     e.printStackTrace();
    30                 }
    31             }*/
    32             // other way
    33             String path="D://a//b//c";
    34             File directory=new File(path);
    35             if(!directory.exists()){
    36                 boolean ms = directory.mkdirs();
    37                 System.out.println("创建:"+ms);
    38             }else{
    39                 System.out.println("目录已存在,无需创建!");
    40             }
    41             String fileName="abc.txt";
    42             File myFile = new File(path, fileName);
    43             if(!myFile.exists()){
    44                 boolean flag;
    45                 try {
    46                     flag = myFile.createNewFile();
    47                     if(flag){
    48                         System.out.println("success!");
    49                     }else{
    50                         System.out.println("defeat!");
    51                     }
    52                 } catch (IOException e) {
    53                     e.printStackTrace();
    54                 }
    55             }
    56         }
    57     }
  • 相关阅读:
    LinkedList源码分析
    Hashtable源码分析
    String源码分析
    记一次ArrayList产生的线上OOM问题
    【spring源码分析】IOC容器初始化——查漏补缺(五)
    前端面试的那些事儿(1)~JavaScript 原始数据类型
    前端面试的那些事儿(2)~ 不再害怕被问 JavaScript 对象
    第二周技术周报-前端的自我修养
    第一周技术周报-前端框架演变
    JavaScript数据类型检测 数组(Array)检测方式
  • 原文地址:https://www.cnblogs.com/57rongjielong/p/9480043.html
Copyright © 2020-2023  润新知