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

Example analysis of File class in java

PHPz
Release: 2023-04-26 19:25:16
forward
1212 people have browsed it

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 {
    }
  }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template