Java 创建文件、文件夹以及临时文件

package Test;

import java.io.File;
import java.io.IOException;
public class CreateFileTest {
/**
* 创建单个文件
* @param destFileName 文件名
* @return 创建成功返回true,否则返回false
*/
public static boolean CreateFile(String destFileName) {
   File file = new File(destFileName);
   
   if (file.exists()) {
    System.out.println("创建单个文件" + destFileName + "失败,目标文件已存在!");
    return false;
   }
   
   
   if (destFileName.endsWith(File.separator)) {
    System.out.println("创建单个文件" + destFileName + "失败,目标不能是目录!");
    return false;
   }
   
   if (!file.getParentFile().exists()) {
    System.out.println("目标文件所在路径不存在,准备创建。。。");
    if (!file.getParentFile().mkdirs()) {
   
    System.out.println("创建目录文件所在的目录失败!");
    return false;
    }
   }
   
// 创建目标文件
   try {
    if (file.createNewFile()) {
    System.out.println("创建单个文件" + destFileName + "成功!");
    return true;
    } else {
    System.out.println("创建单个文件" + destFileName + "失败!");
    return false;
    }
   } catch (IOException e) {
    e.printStackTrace();
    System.out.println("创建单个文件" + destFileName + "失败!");
    return false;
   }
}
/**
* 创建目录
* @param destDirName 目标目录名
* @return 目录创建成功返回true,否则返回false
*/
public static boolean createDir(String destDirName) {
   File dir = new File(destDirName);
   if(dir.exists()) {
    System.out.println("创建目录" + destDirName + "失败,目标目录已存在!");
    return false;
   }
   if(!destDirName.endsWith(File.separator))
    destDirName = destDirName + File.separator;
   // 创建单个目录
   if(dir.mkdirs()) {
    System.out.println("创建目录" + destDirName + "成功!");
    return true;
   } else {
    System.out.println("创建目录" + destDirName + "成功!");
    return false;
   }
}
/**
* 创建临时文件
* @param prefix 临时文件的前缀
* @param suffix 临时文件的后缀
* @param dirName 临时文件所在的目录,如果输入null,则在用户的文档目录下创建临时文件
* @return 临时文件创建成功返回抽象路径名的规范路径名字符串,否则返回null
*/
public static String createTempFile(String prefix, String suffix, String dirName) {
   File tempFile = null;
   try{
    if(dirName == null) {
    // 在默认文件夹下创建临时文件
    tempFile = File.createTempFile(prefix, suffix);
    return tempFile.getCanonicalPath();
    }else {
    File dir = new File(dirName);
    // 如果临时文件所在目录不存在,首先创建
    if(!dir.exists()) {
    if(!CreateFileTest.createDir(dirName)){
    System.out.println("创建临时文件失败,不能创建临时文件所在目录!");
    return null;
    }
    }
    tempFile = File.createTempFile(prefix, suffix, dir);
    return tempFile.getCanonicalPath();
    }
   
   } catch(IOException e) {
    e.printStackTrace();
    System.out.println("创建临时文件失败" + e.getMessage());
    return null;
   }
}
public static void main(String[] args) {
// 创建目录
   String dirName = "d:/test/test0/test1";
   CreateFileTest.createDir(dirName);
   // 创建文件
   String fileName = dirName + "/test2/testFile.txt";
   CreateFileTest.CreateFile(fileName);
   // 创建临时文件
   String prefix = "temp";
   String suffix = ".txt";
   for(int i = 0; i < 10; i++) {
    System.out.println("创建了临时文件:" + CreateFileTest.createTempFile(prefix, suffix, dirName));
   }
}
}
posted @   glose  阅读(8127)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
< 2011年8月 >
31 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 1 2 3
4 5 6 7 8 9 10
点击右上角即可分享
微信分享提示