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

怒放吧生命双鱼

怒放吧生命双鱼

2016-02-19 10:32

每个人都希望每天都是开心的,不要因为一些琐事扰乱了心情还,闲暇的时间怎么打发,关注图老师可以让你学习更多的好东西,下面为大家推荐Java创建文件夹及文件实例代码,赶紧看过来吧!

代码如下:

package com.xhkj.util;

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

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

public class CreateFileUtil {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

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

}

}

展开更多 50%)
分享

猜你喜欢

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

编程语言 网络编程
Java创建文件夹及文件实例代码

Excel 批量创建文件夹

文件夹 电脑入门
Excel 批量创建文件夹

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

adobe dreamweaver教程创建文件夹

Dreamweaver 电脑应用
adobe dreamweaver教程创建文件夹

excel批量创建新建文件夹?

excel
excel批量创建新建文件夹?

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

iPhone在文件夹内怎么新建文件夹

iphone iPhone 5s iPhone 6 iphone刷机
iPhone在文件夹内怎么新建文件夹

乐视云盘怎么创建文件夹

手机软件 应用软件
乐视云盘怎么创建文件夹

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

怎样删除系统服务

怎样删除系统服务

MYSQL基础之连接MYSQL、修改密码、添加用户

MYSQL基础之连接MYSQL、修改密码、添加用户
下拉加载更多内容 ↓