【Java-IO】Java檔案操作

來源:互聯網
上載者:User

標籤:file

File

File能建立,刪除,重新命名檔案和目錄,但是不能查看檔案內容。

構造方法
public File(string pathName);//以path路徑建立對象
public File(String parent, String child);//以parent為父路徑,child為子路徑建立File對象

UNIX下路徑分隔字元為’/’, windows為’\’或者’/’

一個程式瞭解File類
//在D盤建立路徑加檔案        File d1= new File("D:/dir1");//建立d1路徑            if(!d1.exists()) {                d1.mkdir();//如果d1路徑不存在就建立路徑            }            File d2 = new File(d1, "d2");//建立d2路徑            if(!d2.exists()) {                d2.mkdirs();如果d2路徑不存在就建立基於d1路徑為父路徑的路徑            }            File file = new File(d2, "text.txt");//建立一個有file對象的檔案            if(!file.exists()) {                try {                    file.createNewFile();//建立檔案                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }

【Java-IO】Java檔案操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.