Java創(chuàng)建文件夾及文件實(shí)例代碼
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)建單個(gè)文件" + destFileName + "失敗,目標(biāo)文件已存在!");
return false;
}
if (destFileName.endsWith(File.separator)) {
System.out.println("創(chuàng)建單個(gè)文件" + destFileName + "失敗,目標(biāo)不能是目錄!");
return false;
}
if (!file.getParentFile().exists()) {
System.out.println("目標(biāo)文件所在路徑不存在,準(zhǔn)備創(chuàng)建。。。");
if (!file.getParentFile().mkdirs()) {
System.out.println("創(chuàng)建目錄文件所在的目錄失敗!");
return false;
}
}
// 創(chuàng)建目標(biāo)文件
try {
if (file.createNewFile()) {
System.out.println("創(chuàng)建單個(gè)文件" + destFileName + "成功!");
return true;
} else {
System.out.println("創(chuàng)建單個(gè)文件" + destFileName + "失??!");
return false;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("創(chuàng)建單個(gè)文件" + destFileName + "失??!");
return false;
}
}
public static boolean createDir(String destDirName) {
File dir = new File(destDirName);
if(dir.exists()) {
System.out.println("創(chuàng)建目錄" + destDirName + "失敗,目標(biāo)目錄已存在!");
return false;
}
if(!destDirName.endsWith(File.separator))
destDirName = destDirName + File.separator;
// 創(chuàng)建單個(gè)目錄
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) {
// 在默認(rèn)文件夾下創(chuàng)建臨時(shí)文件
tempFile = File.createTempFile(prefix, suffix);
return tempFile.getCanonicalPath();
}
else {
File dir = new File(dirName);
// 如果臨時(shí)文件所在目錄不存在,首先創(chuàng)建
if(!dir.exists()) {
if(!CreateFileUtil.createDir(dirName)){
System.out.println("創(chuàng)建臨時(shí)文件失敗,不能創(chuàng)建臨時(shí)文件所在目錄!");
return null;
}
}
tempFile = File.createTempFile(prefix, suffix, dir);
return tempFile.getCanonicalPath();
}
} catch(IOException e) {
e.printStackTrace();
System.out.println("創(chuàng)建臨時(shí)文件失敗" + 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)建臨時(shí)文件
String prefix = "temp";
String suffix = ".txt";
for(int i = 0; i < 10; i++) {
System.out.println("創(chuàng)建了臨時(shí)文件:" + CreateFileUtil.createTempFile(prefix, suffix, dirName));
}
}
}
- java實(shí)現(xiàn)ftp上傳 如何創(chuàng)建文件夾
- Java讀寫(xiě)文件創(chuàng)建文件夾多種方法示例詳解
- Java 如何實(shí)現(xiàn)解壓縮文件和文件夾
- java 通過(guò) SmbFile 類(lèi)操作共享文件夾的示例
- 使用java API實(shí)現(xiàn)zip遞歸壓縮和解壓文件夾
- Java使用遞歸復(fù)制文件夾及文件夾
- Java拷貝文件夾和刪除文件夾代碼實(shí)例
- Java實(shí)現(xiàn)把文件及文件夾壓縮成zip
- Java移動(dòng)文件夾及其所有子文件與子文件夾
- 詳細(xì)總結(jié)Java創(chuàng)建文件夾的方法及優(yōu)缺點(diǎn)
相關(guān)文章
SpringBoot中的ApplicationListener事件監(jiān)聽(tīng)器使用詳解
這篇文章主要介紹了SpringBoot中的ApplicationListener事件監(jiān)聽(tīng)器使用詳解,ApplicationListener是應(yīng)用程序的事件監(jiān)聽(tīng)器,繼承自java.util.EventListener標(biāo)準(zhǔn)接口,采用觀察者設(shè)計(jì)模式,需要的朋友可以參考下2023-11-11JavaWeb 使用Session實(shí)現(xiàn)一次性驗(yàn)證碼功能
這篇文章主要介紹了JavaWeb 使用Session實(shí)現(xiàn)一次性驗(yàn)證碼功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08Java設(shè)計(jì)模式之責(zé)任鏈模式(Chain of Responsibility模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之責(zé)任鏈模式(Chain of Responsibility模式)介紹,本文講解了如何使用責(zé)任鏈模式,并給出了4種使用實(shí)例,需要的朋友可以參考下2015-03-03Java中八種基本數(shù)據(jù)類(lèi)型的默認(rèn)值
這篇文章主要介紹了Java中八種基本數(shù)據(jù)類(lèi)型的默認(rèn)值 的相關(guān)資料,需要的朋友可以參考下2016-07-07Java多線(xiàn)程中常見(jiàn)的幾個(gè)問(wèn)題
這篇文章主要介紹了Java多線(xiàn)程中常見(jiàn)的幾個(gè)問(wèn)題 ,需要的朋友可以參考下2015-05-05關(guān)于重寫(xiě)equals()方法和hashCode()方法及其簡(jiǎn)單的應(yīng)用
這篇文章主要介紹了關(guān)于重寫(xiě)equals()方法和hashCode()方法及其簡(jiǎn)單的應(yīng)用,網(wǎng)上的知識(shí)有些可能是錯(cuò)誤的,關(guān)于?equals()?方法的理解,大家討論不一樣,需要的朋友可以參考下2023-04-04關(guān)于Spring中的@Configuration中的proxyBeanMethods屬性
這篇文章主要介紹了關(guān)于Spring中的@Configuration中的proxyBeanMethods屬性,需要的朋友可以參考下2023-07-07