Use of file in Java

Source: Internet
Author: User

 PackageShi;ImportJava.io.File;Importjava.io.IOException;ImportJava.util.Calendar; Public classTest { Public Static voidMain (string[] args)throwsIOException {File f=NewFile ("F:/test/javakc.txt");//test whether the file can be executed        Booleanb1=F.canexecute (); System.out.println (B1);//determine if a file exists        BooleanB2=f.exists (); System.out.println (B2);//when creating a new file, throw an exception to determine if the file already existsf.createnewfile ();//gets the absolute path of the fileString s1=F.getabsolutepath (); System.out.println ("The absolute path to the file is" +s1);//get the remaining space on the disk where the file resides        Longl1=F.getfreespace (); System.out.println ("The remaining space on the disk where the file resides is" +L1);//gets the name of the fileString s2=F.getname (); System.out.println ("The name of the file is" +S2);//get the parent directory of the fileString s3=f.getparent (); System.out.println ("The first method gets the directory of files is" +S3); File File=F.getparentfile (); System.out.println ("The second method obtains the file parent directory is" +file);//the full path of the file isString s4=F.getabsolutepath (); System.out.println ("The full path of the file is" +S4);//determine if the file is a standard file        Booleanb3=F.isfile (); System.out.println ("Determine if the file is a standard file" +b3);//determine if the file is not a directory and judge whether the standard file is reversed        Booleanb4=f.isdirectory (); System.out.println ("Judging if the file is not a directory" +b4);//determine if the file is hidden        Booleanb5=F.ishidden (); System.out.println ("Determine if the file is hidden" +b5);//The time the file was last modified, the return value is the time from Greenwich        LongL2=f.lastmodified ();//System.out.println (L2);//GMT//GMT ChangeCalendar C =calendar.getinstance ();        C.settimeinmillis (L2); intYear =C.get (calendar.year); //Note: Month is special, starting from 0, that is, 0 for January        intmonth =C.get (Calendar.month); intDay =C.get (Calendar.day_of_month); intH=C.get (Calendar.hour_of_day); intm=C.get (Calendar.minute); ints=C.get (calendar.second);//System.out.println (c);System.out.println ("Set time is" + year + "years" + (month + 1) + "month" + day+ "Day" +h+ "+m+" minute "+s+" seconds ");//get the length of the file        Longl4=f.length (); System.out.println ("The length of the file is" +L4);//Modify the name of the file good trouble dripping ..... File dest=NewFile ("F:/test/java good."); Booleanb6=F.renameto (dest); System.out.println ("The file name was successfully modified:" +b6); System.out.println (Dest.getname ());//file read and Write permissionsF.setwritable (false); //Create a directoryFile f2=NewFile ("F:/test");//get sub-files in directorySystem.out.print ("The directory in the file is:"); String str[]=f2.list ();  for(String str2:str) {System.out.print (str2+","); }                                //Deleting Files        Booleanb9=F.delete (); System.out.println ("File is deleted" +b3); }}

Use of file in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.