Java創(chuàng)建文件夾及文件實例代碼
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("創(chuàng)建單個文件" + destFileName + "失敗,目標文件已存在!");
return false;
}
if (destFileName.endsWith(File.separator)) {
System.out.println("創(chuàng)建單個文件" + destFileName + "失敗,目標不能是目錄!");
return false;
}
if (!file.getParentFile().exists()) {
System.out.println("目標文件所在路徑不存在,準備創(chuàng)建。。。");
if (!file.getParentFile().mkdirs()) {
System.out.println("創(chuàng)建目錄文件所在的目錄失敗!");
return false;
}
}
// 創(chuàng)建目標文件
try {
if (file.createNewFile()) {
System.out.println("創(chuàng)建單個文件" + destFileName + "成功!");
return true;
} else {
System.out.println("創(chuàng)建單個文件" + destFileName + "失敗!");
return false;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("創(chuàng)建單個文件" + destFileName + "失敗!");
return false;
}
}
public static boolean createDir(String destDirName) {
File dir = new File(destDirName);
if(dir.exists()) {
System.out.println("創(chuàng)建目錄" + destDirName + "失敗,目標目錄已存在!");
return false;
}
if(!destDirName.endsWith(File.separator))
destDirName = destDirName + File.separator;
// 創(chuàng)建單個目錄
if(dir.mkdirs()) {
System.out.println("創(chuàng)建目錄" + destDirName + "成功!");
return true;
} else {
System.out.println("創(chuàng)建目錄" + destDirName + "成功!");
return false;
}
}
public static String createTempFile(String prefix, String suffix, String dirName) {
File tempFile = null;
try{
if(dirName == null) {
// 在默認文件夾下創(chuàng)建臨時文件
tempFile = File.createTempFile(prefix, suffix);
return tempFile.getCanonicalPath();
}
else {
File dir = new File(dirName);
// 如果臨時文件所在目錄不存在,首先創(chuàng)建
if(!dir.exists()) {
if(!CreateFileUtil.createDir(dirName)){
System.out.println("創(chuàng)建臨時文件失敗,不能創(chuàng)建臨時文件所在目錄!");
return null;
}
}
tempFile = File.createTempFile(prefix, suffix, dir);
return tempFile.getCanonicalPath();
}
} catch(IOException e) {
e.printStackTrace();
System.out.println("創(chuàng)建臨時文件失敗" + e.getMessage());
return null;
}
}
public static void main(String[] args) {
// 創(chuàng)建目錄
String dirName = "c:/test/test0/test1";
CreateFileUtil.createDir(dirName);
// 創(chuàng)建文件
String fileName = dirName + "/test2/testFile.txt";
CreateFileUtil.CreateFile(fileName);
// 創(chuàng)建臨時文件
String prefix = "temp";
String suffix = ".txt";
for(int i = 0; i < 10; i++) {
System.out.println("創(chuàng)建了臨時文件:" + CreateFileUtil.createTempFile(prefix, suffix, dirName));
}
}
}
相關(guān)文章
SpringBoot中的ApplicationListener事件監(jiān)聽器使用詳解
這篇文章主要介紹了SpringBoot中的ApplicationListener事件監(jiān)聽器使用詳解,ApplicationListener是應(yīng)用程序的事件監(jiān)聽器,繼承自java.util.EventListener標準接口,采用觀察者設(shè)計模式,需要的朋友可以參考下2023-11-11JavaWeb 使用Session實現(xiàn)一次性驗證碼功能
這篇文章主要介紹了JavaWeb 使用Session實現(xiàn)一次性驗證碼功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08Java設(shè)計模式之責任鏈模式(Chain of Responsibility模式)介紹
這篇文章主要介紹了Java設(shè)計模式之責任鏈模式(Chain of Responsibility模式)介紹,本文講解了如何使用責任鏈模式,并給出了4種使用實例,需要的朋友可以參考下2015-03-03關(guān)于重寫equals()方法和hashCode()方法及其簡單的應(yīng)用
這篇文章主要介紹了關(guān)于重寫equals()方法和hashCode()方法及其簡單的應(yīng)用,網(wǎng)上的知識有些可能是錯誤的,關(guān)于?equals()?方法的理解,大家討論不一樣,需要的朋友可以參考下2023-04-04關(guān)于Spring中的@Configuration中的proxyBeanMethods屬性
這篇文章主要介紹了關(guān)于Spring中的@Configuration中的proxyBeanMethods屬性,需要的朋友可以參考下2023-07-07