Home > Java > javaTutorial > body text

Example analysis of java folder operations

PHPz
Release: 2023-04-18 12:07:04
forward
1340 people have browsed it

Folder operation

package com.file;

import java.io.File;

/**
 * Created by elijahliu on 2017/2/11.
 */
public class HelloFolder {
  public static void main(String[] args) {
    File folder = new File("my new folder");
    if (folder.mkdir()) {//创建文件夹 判断是否成功
      System.out.println("文件夹创建完成");
      File newfolder = new File("myn new foleder - new");
      folder.renameTo(newfolder);//这里重命名了文件夹 文件夹的重命名是可以单独更改一级的文件夹名的 而这一级下面的文件夹不变 保存目录结构
      if (folder.delete()) {
        System.out.print("done");//这里的删除只能删除空文件夹,如果文件夹中有东西,那么则不能删除,不问三七二十一直接删除一个非空文件夹是非常不负责任的
      } else {
        System.out.println("fail");
      }

    }else{
      if (folder.exists()) {
        System.out.println("文件夹已经存在不用创建");
      }else{
        System.out.println("文件夹创建失败");
      }
    }
    File folders = new File("my new folder/one/two/three/main");
    folders.mkdirs();//在java中用mkdir只能创建一个,mkdirs可以创建多级目录

  }
}
Copy after login

The above is the detailed content of Example analysis of java folder operations. 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