ad file type not recognised_Java实用工具类:File工具类方法学习,可创建目录及文件...
開發項目過程中,會用到很多工具類,今天分享一個Java中File操作工具類,可以幫你節約時間,提高開發效率。
File工具類
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 + "失敗!" + e.getMessage());
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;
if (dirName == null) {
try{
//在默認文件夾下創建臨時文件
tempFile = File.createTempFile(prefix, suffix);
//返回臨時文件的路徑
return tempFile.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
System.out.println("創建臨時文件失敗!" + e.getMessage());
return null;
}
} else {
File dir = new File(dirName);
//如果臨時文件所在目錄不存在,首先創建
if (!dir.exists()) {
if (!CreateFileUtil.createDir(dirName)) {
System.out.println("創建臨時文件失敗,不能創建臨時文件所在的目錄!");
return null;
}
}
try {
//在指定目錄下創建臨時文件
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 = "D:/work/temp/temp0/temp1";
CreateFileUtil.createDir(dirName);
//創建文件
String fileName = dirName + "/temp2/tempFile.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));
}
//在默認目錄下創建臨時文件
for (int i = 0; i < 10; i++) {
System.out.println("在默認目錄下創建了臨時文件:"
+ CreateFileUtil.createTempFile(prefix, suffix, null));
}
}
}
以上,是我實際項目中用過的File工具類,僅供參考,有什么好的方法,可以評論區交流。
我是一名碼齡10年的程序員,在這里會分享實在干貨,讓你少走彎路,成就精彩人生。
總結
以上是生活随笔為你收集整理的ad file type not recognised_Java实用工具类:File工具类方法学习,可创建目录及文件...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: roc曲线怎么绘制_ROC曲线和PR曲线
- 下一篇: java的迭代器类中有哪些类_java中