国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java創建文件夾及文件實例代碼

2019-11-26 16:11:23
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

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

}

}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绥化市| 正镶白旗| 安福县| 石泉县| 故城县| 陵水| 沙洋县| 景德镇市| 凤台县| 土默特右旗| 宣恩县| 蛟河市| 十堰市| 马边| 大余县| 中卫市| 顺昌县| 九龙县| 宜兰市| 德惠市| 容城县| 奎屯市| 巧家县| 胶州市| 邳州市| 和田市| 社会| 马尔康县| 潜江市| 平邑县| 酒泉市| 长乐市| 贵南县| 宜黄县| 冕宁县| 京山县| 襄汾县| 道真| 从化市| 武陟县| 赣榆县|