Java:文件、目录的创建与删除

创建文件对象的构造器:

new File(String pathname); //根据路径创建对象
new File(File parent, String pathname); //根据父目录文件和子路径创建对象
new File(String parent, String child); //根据父路径和子路径创建对象

File类中创建新文件的方法createNewFile()源码:

public boolean createNewFile() throws IOException {
	SecurityManager security = System.getSecurityManager();
	if (security != null) security.checkWrite(path);
	if (isInvalid()) {
	throw new IOException("Invalid file path");
	}
	return fs.createFileExclusively(path);
}

实现新文件的创建:

package com.wyz.test;

import org.junit.Test;
import java.io.File;
import java.io.IOException;

public class test {
    public static void main(String[] args) {
    }
    @Test
    public void createFile1() {
        //第一种创建方式
        String path = "E:\\test1.txt";
        File file = new File(path);
        try {
            file.createNewFile();
            System.out.println("创建成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void createFile2(){
        //第二种创建方式
        File parentFile = new File("E:\\");
        String path = "test2.txt";
        File file = new File(parentFile, path);
        try {
            file.createNewFile();
            System.out.println("创建成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void createFile3(){
        //第三种创建方式
        String parentPath = "E:\\";
        String path = "test3.txt";
        File file = new File(parentPath, path);
        try {
            file.createNewFile();
            System.out.println("创建成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

实现文件和目录的删除:
注意delete()只能删除文件或者空目录,不能删除含有文件的目录!

    @Test
    public void deleteFile(){
        //判断文件是否存在,如果存在就删除
        String path = "E:\\test1.txt";
        File file = new File(path);
        if(file.exists()){
            if(file.delete()){
                System.out.println(path + "删除成功!");
            } else {
                System.out.println("删除失败!");
            }
        } else {
            System.out.println(path + "不存在!");
        }
    }
    @Test
    public void deleteDirectory(){
        //判断目录是否存在,如果存在就删除
        File file = new File("E:\\test");
        if(file.exists()){
            if(file.delete()){
                System.out.println("删除成功!");
            } else {
                System.out.println("删除失败!");
            }
        } else {
            System.out.println("该路径不存在!");
        }
    }

实现创建多级目录(创建单级目录使用mkdir()和mkdirs()都可以):

    @Test
    public void makeDirectory(){
        //判断多级目录是否存在,如果不存在就创建
        File file = new File("E:\\test\\a\\b\\c");
        if(!file.exists()){
            if(file.mkdirs()){
                System.out.println("创建成功!");
            } else {
                System.out.println("创建失败!");
            }
        } else {
            System.out.println("该路径已存在!");
        }
    }
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wistain

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值