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配置logback日志管理過(guò)程詳解
這篇文章主要介紹了springboot配置logback日志管理過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Java以struts2為例介紹如何實(shí)現(xiàn)圖片上傳
這篇文章主要介紹了Java struts2中如何實(shí)現(xiàn)圖片上傳的相關(guān)資料,需要的朋友可以參考下2015-11-11
JAVA代碼設(shè)置selector不同狀態(tài)下的背景顏色
這篇文章主要介紹了JAVA代碼設(shè)置selector不同狀態(tài)下的背景顏色,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05
Java通過(guò)stmp協(xié)議發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了Java通過(guò)stmp協(xié)議發(fā)送郵件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
java GUI編程之布局控制器(Layout)實(shí)例分析
這篇文章主要介紹了java GUI編程之布局控制器(Layout),結(jié)合實(shí)例形式分析了java GUI編程中布局控制器(Layout)具體功能、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-01-01
SpringBoot2.0整合WebSocket代碼實(shí)例
這篇文章主要介紹了SpringBoot2.0整合WebSocket代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
springboot使用redisTemplate操作lua腳本
本文主要介紹了springboot使用redisTemplate操作lua腳本,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

