java創(chuàng)建文件和目錄

2018-06-20 16:48 更新

創(chuàng)建文件和目錄的關(guān)鍵技術(shù)點(diǎn)如下:

File類的createNewFile根據(jù)抽象路徑創(chuàng)建一個(gè)新的空文件,當(dāng)抽象路徑制定的文件存在時(shí),創(chuàng)建失敗
File類的mkdir方法根據(jù)抽象路徑創(chuàng)建目錄
File類的mkdirs方法根據(jù)抽象路徑創(chuàng)建目錄,包括創(chuàng)建必需但不存在的父目錄
File類的createTempFile方法創(chuàng)建臨時(shí)文件,可以制定臨時(shí)文件的文件名前綴、后綴及文件所在的目錄,如果不指定目錄,則存放在系統(tǒng)的臨時(shí)文件夾下。
除mkdirs方法外,以上方法在創(chuàng)建文件和目錄時(shí),必須保證目標(biāo)文件不存在,而且父目錄存在,否則會(huì)創(chuàng)建失敗

實(shí)例演示

  1. package book.io;
  2. import java.io.File;
  3. import java.io.IOException;
  4. 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;
}
//判斷目標(biāo)文件所在的目錄是否存在
if(!file.getParentFile().exists()) {
//如果目標(biāo)文件所在的目錄不存在,則創(chuàng)建父目錄
System.out.println("目標(biāo)文件所在目錄不存在,準(zhǔn)備創(chuàng)建它!");
if(!file.getParentFile().mkdirs()) {
System.out.println("創(chuàng)建目標(biāo)文件所在目錄失敗!");
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 + "失??!" + e.getMessage());
return false;
}
}

  1. public static boolean createDir(String destDirName) {
  2. File dir = new File(destDirName);
  3. if (dir.exists()) {
  4. System.out.println("創(chuàng)建目錄" + destDirName + "失敗,目標(biāo)目錄已經(jīng)存在");
  5. return false;
  6. }
  7. if (!destDirName.endsWith(File.separator)) {
  8. destDirName = destDirName + File.separator;
  9. }
  10. //創(chuàng)建目錄
  11. if (dir.mkdirs()) {
  12. System.out.println("創(chuàng)建目錄" + destDirName + "成功!");
  13. return true;
  14. } else {
  15. System.out.println("創(chuàng)建目錄" + destDirName + "失?。?);
  16. return false;
  17. }
  18. }
  19. public static String createTempFile(String prefix, String suffix, String dirName) {
  20. File tempFile = null;
  21. if (dirName == null) {
  22. try{
  23. //在默認(rèn)文件夾下創(chuàng)建臨時(shí)文件
  24. tempFile = File.createTempFile(prefix, suffix);
  25. //返回臨時(shí)文件的路徑
  26. return tempFile.getCanonicalPath();
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. System.out.println("創(chuàng)建臨時(shí)文件失??!" + e.getMessage());
  30. return null;
  31. }
  32. } else {
  33. File dir = new File(dirName);
  34. //如果臨時(shí)文件所在目錄不存在,首先創(chuàng)建
  35. if (!dir.exists()) {
  36. if (!CreateFileUtil.createDir(dirName)) {
  37. System.out.println("創(chuàng)建臨時(shí)文件失敗,不能創(chuàng)建臨時(shí)文件所在的目錄!");
  38. return null;
  39. }
  40. }
  41. try {
  42. //在指定目錄下創(chuàng)建臨時(shí)文件
  43. tempFile = File.createTempFile(prefix, suffix, dir);
  44. return tempFile.getCanonicalPath();
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. System.out.println("創(chuàng)建臨時(shí)文件失敗!" + e.getMessage());
  48. return null;
  49. }
  50. }
  51. }
  52. public static void main(String[] args) {
  53. //創(chuàng)建目錄
  54. String dirName = "D:/work/temp/temp0/temp1";
  55. CreateFileUtil.createDir(dirName);
  56. //創(chuàng)建文件
  57. String fileName = dirName + "/temp2/tempFile.txt";
  58. CreateFileUtil.createFile(fileName);
  59. //創(chuàng)建臨時(shí)文件
  60. String prefix = "temp";
  61. String suffix = ".txt";
  62. for (int i = 0; i < 10; i++) {
  63. System.out.println("創(chuàng)建了臨時(shí)文件:"
  64. + CreateFileUtil.createTempFile(prefix, suffix, dirName));
  65. }
  66. //在默認(rèn)目錄下創(chuàng)建臨時(shí)文件
  67. for (int i = 0; i < 10; i++) {
  68. System.out.println("在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:"
  69. + CreateFileUtil.createTempFile(prefix, suffix, null));
  70. }
  71. }
  72. }

輸出結(jié)果:

  1. 創(chuàng)建目錄D:/work/temp/temp0/temp1成功!
  2. 目標(biāo)文件所在目錄不存在,準(zhǔn)備創(chuàng)建它!
  3. 創(chuàng)建單個(gè)文件D:/work/temp/temp0/temp1/temp2/tempFile.txt成功!
  4. 創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5171.txt
  5. 創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5172.txt
  6. 創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5173.txt
  7. 創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5174.txt
  8. 創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5175.txt
  9. 創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5176.txt
  10. 創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5177.txt
  11. 創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5178.txt
  12. 創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5179.txt
  13. 創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5180.txt
  14. 在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5181.txt
  15. 在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5182.txt
  16. 在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5183.txt
  17. 在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5184.txt
  18. 在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5185.txt
  19. 在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5186.txt
  20. 在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5187.txt
  21. 在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5188.txt
  22. 在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5189.txt
  23. 在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5190.txt
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)