国产探花免费观看_亚洲丰满少妇自慰呻吟_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));
    }

}

}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定边县| 凯里市| 阿荣旗| 芦溪县| 荣成市| 本溪| 兴仁县| 星子县| 双江| 镇宁| 荔波县| 辽宁省| 汪清县| 新化县| 南华县| 泸定县| 庆阳市| 富蕴县| 平乡县| 新晃| 凤山县| 凤凰县| 宣化县| 忻州市| 腾冲县| 闵行区| 文安县| 江都市| 开封市| 睢宁县| 延川县| 乐平市| 新源县| 商都县| 连江县| 建始县| 谷城县| 姜堰市| 曲阳县| 象州县| 桦川县|