商城首页欢迎来到中国正版软件门户

您的位置:首页 >java中File类的示例分析

java中File类的示例分析

  发布于2023-05-02 阅读(0)

扫一扫,手机访问

File类简介

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 {
    }
  }
}
本文转载于:https://www.yisu.com/zixun/658177.html 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注