我們先來(lái)看下具體代碼:
創(chuàng)新互聯(lián)公司是一家專業(yè)提供略陽(yáng)企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)制作、網(wǎng)站建設(shè)、H5建站、小程序制作等業(yè)務(wù)。10年已為略陽(yáng)眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。
import java.io.File; import java.io.IOException; public class CreatFile{ public static void main(String[] args) { File newDir=new File("D:/test"); //聲明磁盤(pán)目錄 File newFile = new File(newDir,"test.txt"); //聲明目錄文件 boolean newCreatDir=newDir.exists(); boolean newCreatFile=newFile.exists(); //創(chuàng)建目錄及文件 if(newCreatDir==false){ try{ //異常監(jiān)聽(tīng) newDir.mkdirs(); //創(chuàng)建目錄 System.out.println(newDir.getAbsolutePath()+"目錄已創(chuàng)建"); newFile.createNewFile(); //創(chuàng)建文件 System.out.println(newFile.getAbsolutePath()+"文件已創(chuàng)建"); } catch(IOException e){ //捕捉異常 e.printStackTrace(); //在命令行打印異常信息在程序中出錯(cuò)的位置及原因 } } else{ System.out.println(newDir.getAbsolutePath()+"目錄已存在"); } if(newCreatFile==true){ System.out.println(newFile.getAbsolutePath()+"文件已存在"); } } }
說(shuō)明:
創(chuàng)建目錄的方法,mkdirs();或者mkdir(); 區(qū)別在于mkdirs()可以多級(jí)創(chuàng)建。
創(chuàng)建文件方法,createNewFile();
文章標(biāo)題:JavaIO創(chuàng)建目錄和文件實(shí)例代碼
分享鏈接:http://jinyejixie.com/article18/jojddp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、App開(kāi)發(fā)、網(wǎng)頁(yè)設(shè)計(jì)公司、商城網(wǎng)站、企業(yè)建站、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)