java File類的基本使用

來源:互聯網
上載者:User

標籤:通過   number   throws   dem   static   tle   b2c   gravity   comm   

package com.soar.file;import java.io.File;import java.io.IOException;public class Demo2_FileMethod {/*    * A:建立功能        * public boolean createNewFile():建立檔案 如果存在這樣的檔案,就不建立了        * public boolean mkdir():建立檔案夾 如果存在這樣的檔案夾,就不建立了        * public boolean mkdirs():建立檔案夾,如果父資料夾不存在,會幫你建立出來    * B:案例示範        * File類的建立功能        * 注意事項:            * 如果你建立檔案或者檔案夾忘了寫盤符路徑,那麼,預設在項目路徑下。 */    public static void main(String[] args) throws IOException {        //createNewFile();        File dir1 = new File("aaa");        System.out.println(dir1.mkdirs()); //true        File dir2 = new File("bbb.txt");    //這樣寫也是可以的,檔案夾也是可以有尾碼的        System.out.println(dir2.mkdirs()); //true        File dir3 = new File("ccc\\ddd");           System.out.println(dir3.mkdirs());  //建立多級目錄(檔案夾)    }private static void createNewFile() throws IOException {    File file = new File("yyy.txt");    System.out.println(file.createNewFile());   //如果沒有就建立,返回true,如果有就不建立,返回false    File file2 = new File("zzz");              //不加尾碼也可以建立    System.out.println(file2.createNewFile());  //true}}

在該項目下通過代碼建立的檔案及檔案夾

java File類的基本使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.