java中File的使用

來源:互聯網
上載者:User

標籤:

package shi;import java.io.File;import java.io.IOException;import java.util.Calendar;public class Test {        public static void main(String[] args) throws IOException {        File f=new File("F:/test/javakc.txt");//        測試檔案是否可以執行        boolean b1=f.canExecute();        System.out.println(b1);//        判斷檔案是否存在        boolean b2=f.exists();        System.out.println(b2);//        建立新的檔案時,拋出異常,判斷檔案是否已存在        f.createNewFile();//        擷取檔案的絕對路徑        String s1=f.getAbsolutePath();        System.out.println("檔案的絕對路徑是"+s1);//        擷取檔案所在磁碟的剩餘空間        long l1=f.getFreeSpace();        System.out.println("檔案所在磁碟的剩餘空間是"+l1);//        擷取檔案的名稱        String s2=f.getName();        System.out.println("檔案的名稱是"+s2);//        獲得檔案的父目錄        String s3=f.getParent();        System.out.println("第一種方法獲得的檔案的目錄是"+s3);        File file=f.getParentFile();        System.out.println("第二種方法獲得的檔案父目錄是"+file);//        檔案的全路徑是        String s4=f.getAbsolutePath();        System.out.println("檔案的全路徑是"+s4);//        判斷檔案是否是標準檔案        boolean b3=f.isFile();        System.out.println("判斷檔案是否是標準檔案"+b3);//        判斷檔案是不是目錄與判斷是否標準檔案相反        boolean b4=f.isDirectory();        System.out.println("判斷檔案是不是目錄"+b4);//        判斷檔案是否隱藏        boolean b5=f.isHidden();        System.out.println("判斷檔案是否隱藏"+b5);//        檔案最後修改的時間,傳回值是距離格林威治的時間        long l2=f.lastModified();//        System.out.println(l2);//格林威治時間//        格林威治時間准換                Calendar c = Calendar.getInstance();         c.setTimeInMillis(l2);        int year = c.get(Calendar.YEAR);        //注意:month特殊,是從0開始的,也就是0表示1月        int month = c.get(Calendar.MONTH);        int day = c.get(Calendar.DAY_OF_MONTH);        int h=c.get(Calendar.HOUR_OF_DAY);        int m=c.get(Calendar.MINUTE);        int s=c.get(Calendar.SECOND);//        System.out.println(c);        System.out.println("設定的時間是" + year + "年" + (month + 1) + "月" + day+ "日"                            +h+"時"+m+"分"+s+"秒");//        獲得檔案的長度        long l4=f.length();        System.out.println("檔案的長度是"+l4);//        修改檔案的名稱    好麻煩滴。。。。。。。。。。。。。        File dest=new File("F:/test/java好吃麼.txt");        boolean b6=f.renameTo(dest);        System.out.println("是否成功修改檔案名稱:"+b6);                System.out.println(dest.getName());//        檔案讀寫權限        f.setWritable(false);                                                        //        建立目錄        File f2=new File("F:/test");//        獲得目錄中的子檔案        System.out.print("檔案中的目錄有:");        String str[]=f2.list();        for(String str2:str){            System.out.print(str2+",");        }                                //        刪除檔案        boolean b9=f.delete();        System.out.println("檔案是否刪除"+b3);                                            }}

 

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.