Java基础-创建文件夹/创建文件

package com.hspedu.io_;

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

public class TestCreateFile {
    /**
     * 创建文件夹目录、创建文件
     * @throws IOException
     */
    @Test
    public void createFile() throws IOException {
        String directoryPath = "e:\\JavaIO\\CreateFile\\";
        File directoryFile = new File(directoryPath);
        String fileName = "createNewFile.txt";
        File file = new File(directoryPath, fileName);
        if (!directoryFile.exists()) {
            directoryFile.mkdirs();
        }
        file.createNewFile();
    }
}

 注意:文件目录也应该看作是一个文件File对象

posted @ 2022-04-14 10:18  柯南同学  阅读(608)  评论(0编辑  收藏  举报