zoukankan      html  css  js  c++  java
  • 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     }
  • 相关阅读:
    OpenResty
    Jmeter
    kubernetes 中部署 metrics-server
    Jenkins 灰度
    socat管理haproxy以及haproxy调优
    代码质量测试工具SonarQube安装配置
    Jenkins+主从+Pipeline+Webhook
    xtrabackup 实现MySQL数据库备份
    idea Error:java: Compilation failed: internal java compiler error
    使用TableSnapshotInputFormat读取Hbase快照数据
  • 原文地址:https://www.cnblogs.com/57rongjielong/p/9480043.html
Copyright © 2011-2022 走看看