Java创建文件夹及文件实例代码

 更新时间:2013年04月17日 21:50:54   作者:  
Java创建文件夹及文件实例代码,需要的朋友可以参考一下

复制代码 代码如下:

package com.xhkj.util;

import java.io.File;
import java.io.IOException;

public class CreateFileUtil {

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;
    }
}

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;
    }
}

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(!CreateFileUtil.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 = "c:/test/test0/test1";
    CreateFileUtil.createDir(dirName);
    // 创建文件
    String fileName = dirName + "/test2/testFile.txt";
    CreateFileUtil.CreateFile(fileName);
    // 创建临时文件
    String prefix = "temp";
    String suffix = ".txt";
    for(int i = 0; i < 10; i++) {
     System.out.println("创建了临时文件:" + CreateFileUtil.createTempFile(prefix, suffix, dirName));
    }

}

}

相关文章

  • springboot中使用@NotNull注解无效解决方法

    springboot中使用@NotNull注解无效解决方法

    这篇文章主要给大家介绍了关于springboot中使用@NotNull注解无效的解决方法,进行参数校验的时候,加了@NotNull注解,@Validated注解和@Valid注解,但是参数校验的时候不生效,需要的朋友可以参考下
    2023-08-08
  • Springboot整合Shiro之加盐MD5加密的方法

    Springboot整合Shiro之加盐MD5加密的方法

    这篇文章主要介绍了Springboot整合Shiro之加盐MD5加密的方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-12-12
  • springboot中报错Invalid character found in the request的解决

    springboot中报错Invalid character found in 

    这篇文章主要介绍了springboot中报错Invalid character found in the request的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • 优化Java代码中if-else的方案分享

    优化Java代码中if-else的方案分享

    代码可读性是衡量代码质量的重要标准,可读性也是可维护性、可扩展性的保证,而我们在编程时常常会发现代码中有大量if else语句,如何进行优化呢,下面就来和大家详细聊聊
    2023-05-05
  • 解读Integer类的parseInt和valueOf的区别

    解读Integer类的parseInt和valueOf的区别

    这篇文章主要介绍了解读Integer类的parseInt和valueOf的区别,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • 在deepin上如何使用Fleet开发SpringBoot 3.0.0项目

    在deepin上如何使用Fleet开发SpringBoot 3.0.0项目

    这篇文章主要介绍了在deepin上使用Fleet开发SpringBoot 3.0.0项目的过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-09-09
  • Java操作Mysql的方法

    Java操作Mysql的方法

    这篇文章主要介绍了Java操作Mysql的方法,实例分析了Java针对有返回结果和没有返回结果的sql操作的相关技巧,需要的朋友可以参考下
    2015-02-02
  • Spring Cloud下基于OAUTH2认证授权的实现示例

    Spring Cloud下基于OAUTH2认证授权的实现示例

    这篇文章主要介绍了Spring Cloud下基于OAUTH2认证授权的实现示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • 浅谈Java三大框架与应用

    浅谈Java三大框架与应用

    这篇文章主要介绍了浅谈Java三大框架与应用,需要的朋友可以参考下
    2015-05-05
  • Mybatis Example的高级用法详解

    Mybatis Example的高级用法详解

    这篇文章主要介绍了Mybatis Example的高级用法详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12

最新评论