Home  >  Article  >  Java  >  Example analysis of File class in java

Example analysis of File class in java

PHPz
PHPzforward
2023-04-26 19:25:161136browse

File class introduction

package com.file;

import java.io.File;
import java.io.IOException;

/**
 * Created by elijahliu on 2017/2/10.
 */
public class filetest {
  public static void main(String[] args) {
    File file = new File("hello.txt");
    //是否存在
    if (file.exists()) {
      //文件
      System.out.println(file.isFile());
      //路径(文件夹)
      System.out.println(file.isDirectory());

      File nameto = new File("new Hello.txt");
      file.renameTo(nameto);//这里就是重命名文件的操作,直接新建一个file对象然后使用renameTo方法可以重命名文件

    } else {
      System.out.println("文件不存在");
      try {
        file.createNewFile();
        System.out.println("文件已被创建");
      } catch (IOException e) {
        System.out.println("文件无法创建");
      }
    }
    if (file.exists()) {
      //删除文件
      file.delete();
      System.out.println("删除文件");
    } else {
    }
  }
}

The above is the detailed content of Example analysis of File class in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete