欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2014年09月19日 11:45:08   投稿:hebedich  
這篇文章主要介紹了淺析java創(chuàng)建文件和目錄的關鍵技術點以及演示示例,是篇非常不錯的文章,有需要的朋友可以參考下

一,創(chuàng)建文件和目錄的關鍵技術點如下:

   1、File類的createNewFile根據抽象路徑創(chuàng)建一個新的空文件,當抽象路徑制定的文件存在時,創(chuàng)建失敗 

     2、File類的mkdir方法根據抽象路徑創(chuàng)建目錄 

     3、File類的mkdirs方法根據抽象路徑創(chuàng)建目錄,包括創(chuàng)建不存在的父目錄 

     4、File類的createTempFile方法創(chuàng)建臨時文件,可以制定臨時文件的文件名前綴、后綴及文件所在的目錄,如果不指定              目錄,則存放在系統(tǒng)的臨時文件夾下。 

      5、除 mkdirs方法外,以上方法在創(chuàng)建文件和目錄時,必須保證目標文件不存在,而且父目錄存在,否則會創(chuàng)建失敗 

二,實例演示如下:

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()) { 
      //如果目標文件所在的目錄不存在,則創(chuàng)建父目錄 
      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 + "失??!" + e.getMessage()); 
      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; 
    if (dirName == null) { 
      try{ 
        //在默認文件夾下創(chuàng)建臨時文件 
        tempFile = File.createTempFile(prefix, suffix); 
        //返回臨時文件的路徑 
        return tempFile.getCanonicalPath(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
        System.out.println("創(chuàng)建臨時文件失??!" + e.getMessage()); 
        return null; 
      } 
    } else { 
      File dir = new File(dirName); 
      //如果臨時文件所在目錄不存在,首先創(chuàng)建 
      if (!dir.exists()) { 
        if (!CreateFileUtil.createDir(dirName)) { 
          System.out.println("創(chuàng)建臨時文件失敗,不能創(chuàng)建臨時文件所在的目錄!"); 
          return null; 
        } 
      } 
      try { 
        //在指定目錄下創(chuàng)建臨時文件 
        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 = "D:/work/temp/temp0/temp1"; 
    CreateFileUtil.createDir(dirName); 
    //創(chuàng)建文件 
    String fileName = dirName + "/temp2/tempFile.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)); 
    } 
    //在默認目錄下創(chuàng)建臨時文件 
    for (int i = 0; i < 10; i++) { 
      System.out.println("在默認目錄下創(chuàng)建了臨時文件:" 
          + CreateFileUtil.createTempFile(prefix, suffix, null)); 
    } 
  } 
 
} 

輸出結果:   

創(chuàng)建目錄D:/work/temp/temp0/temp1成功! 
目標文件所在目錄不存在,準備創(chuàng)建它! 
創(chuàng)建單個文件D:/work/temp/temp0/temp1/temp2/tempFile.txt成功! 
創(chuàng)建了臨時文件:D:work emp emp0 emp1 emp5171.txt 
創(chuàng)建了臨時文件:D:work emp emp0 emp1 emp5172.txt 
創(chuàng)建了臨時文件:D:work emp emp0 emp1 emp5173.txt 
創(chuàng)建了臨時文件:D:work emp emp0 emp1 emp5174.txt 
創(chuàng)建了臨時文件:D:work emp emp0 emp1 emp5175.txt 
創(chuàng)建了臨時文件:D:work emp emp0 emp1 emp5176.txt 
創(chuàng)建了臨時文件:D:work emp emp0 emp1 emp5177.txt 
創(chuàng)建了臨時文件:D:work emp emp0 emp1 emp5178.txt 
創(chuàng)建了臨時文件:D:work emp emp0 emp1 emp5179.txt 
創(chuàng)建了臨時文件:D:work emp emp0 emp1 emp5180.txt 
在默認目錄下創(chuàng)建了臨時文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5181.txt 
在默認目錄下創(chuàng)建了臨時文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5182.txt 
在默認目錄下創(chuàng)建了臨時文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5183.txt 
在默認目錄下創(chuàng)建了臨時文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5184.txt 
在默認目錄下創(chuàng)建了臨時文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5185.txt 
在默認目錄下創(chuàng)建了臨時文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5186.txt 
在默認目錄下創(chuàng)建了臨時文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5187.txt 
在默認目錄下創(chuàng)建了臨時文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5188.txt 
在默認目錄下創(chuàng)建了臨時文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5189.txt 
在默認目錄下創(chuàng)建了臨時文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5190.txt 

相關文章

  • Project?Reactor源碼解析publishOn使用示例

    Project?Reactor源碼解析publishOn使用示例

    這篇文章主要為大家介紹了Project?Reactor源碼解析publishOn使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • Java JDK11的下載與安裝教程

    Java JDK11的下載與安裝教程

    這篇文章主要介紹了Java JDK11的下載與安裝,本文以win10為例給大家講解win10系統(tǒng)下載安裝jdk11的教程,需要的朋友可以參考下
    2023-05-05
  • Java異步非阻塞編程的幾種方式總結

    Java異步非阻塞編程的幾種方式總結

    這篇文章主要介紹了Java異步非阻塞編程的幾種方式總結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 基于request獲取訪問者真實IP代碼示例

    基于request獲取訪問者真實IP代碼示例

    這篇文章主要介紹了基于request獲取訪問者真實IP代碼示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-10-10
  • 利用Java Set 去除重復object的方法

    利用Java Set 去除重復object的方法

    下面小編就為大家?guī)硪黄肑ava Set 去除重復object的方法。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • SpringBoot中使用com.alibaba.druid.filter.config.ConfigTools對數據庫密碼加密的方法

    SpringBoot中使用com.alibaba.druid.filter.config.ConfigTools對數據庫

    這篇文章主要介紹了SpringBoot中使用com.alibaba.druid.filter.config.ConfigTools對數據庫密碼加密的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Java使用多線程異步執(zhí)行批量更新操作方法

    Java使用多線程異步執(zhí)行批量更新操作方法

    這篇文章主要介紹了Java使用多線程異步執(zhí)行批量更新操作,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • 詳解IDEA 中使用Maven創(chuàng)建項目常見錯誤和使用技巧(推薦)

    詳解IDEA 中使用Maven創(chuàng)建項目常見錯誤和使用技巧(推薦)

    這篇文章主要介紹了詳解IDEA 中使用Maven創(chuàng)建項目常見錯誤和使用技巧(推薦),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • spring@value注入配置文件值失敗的原因分析

    spring@value注入配置文件值失敗的原因分析

    這篇文章主要介紹了spring@value注入配置文件值失敗的原因分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring里的Async注解實現異步操作的方法步驟

    Spring里的Async注解實現異步操作的方法步驟

    這篇文章主要介紹了Spring里的Async注解實現異步操作的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04

最新評論