zoukankan      html  css  js  c++  java
  • java-FileDemo

    关于file类的使用:文件的增删查

     1 package com.example;
     2 
     3 import java.io.File;
     4 import java.io.IOException;
     5 
     6 public class FileDemo {
     7     public static void main(String[] args) throws IOException {
     8         File file = new File("."+File.separator+"demo.txt");
     9 
    10         String filename = file.getName();  //获取文件名字
    11 
    12         long length = file.length();  //此file文件在硬盘上占多少 字节
    13 
    14         boolean isFile = file.isFile();  //判断是否文件(还可能是目录)
    15         boolean isDirectory = file.isDirectory();  //判断是否是目录
    16 
    17         /*
    18          * Statement:在当前项目的根目录下创建一个文件
    19          */
    20         File file1 = new File("demo.txt");  //默认在当前目录下
    21         if(!file1.exists()){
    22             file1.createNewFile();  //在当前目录下创建文件demo.txt
    23             System.out.println("创建文件成功");
    24         }
    25         if (file1.exists()){
    26             file1.delete();  //删除demo.txt
    27         }
    28         /*
    29          * Statement:mkdir()创建单级目录
    30          */
    31         File directory = new File("demo");
    32         if(directory.exists()){
    33             directory.mkdir();
    34         }
    35         File dirs = new File("a/b");
    36         dirs.mkdirs();  //创建本级目录同时创建其下属的多级原不存在的目录
    37     }
    38 }
  • 相关阅读:
    常用Git命令清单
    上海金瑢信息有限公司面试
    上海视频面试
    bootstrp-3.0
    B站小姐姐面试分享2
    B站小姐姐分享第一次电话面试
    Array.from()方法就是将一个类数组对象或者可遍历对象转换成一个真正的数组。
    findIndex
    es5,es6
    es6数组去重
  • 原文地址:https://www.cnblogs.com/DeRozan/p/7282180.html
Copyright © 2011-2022 走看看