Javafile Basic Operation

Source: Internet
Author: User
Tags readable

/*** Create a file, directory or multilevel directory *@paramPath *@throwsIOException *@see[Class, Class # method, Class # member]*/       Public Static voidCreateFile (String Path)throwsIOException {if(Path = =NULL){              return ; } File File=NewFile (path); if(Path = =NULL){              return ; } File F=NewFile (path); File fileparent=F.getparentfile (); if(!fileparent.exists ())           {Fileparent.mkdirs (); }          if(!f.exists ())          {F.createnewfile (); }  //if (!file.exists ()) {//File.createnewfile (); //      /*//Create a directory in which the parent directory in the path must exist (D://test)//if (!file.isdirectory ()) {//File.mkdir (); //      }  //      //create a multilevel directory whose path to the parent directory may not exist (D://Test//Test//test)//if (!file.isdirectory ()) {//File.mkdirs (); //      }*/  //      //allow all people to use only readable//file.setreadable (TRUE); //      //only allow all people to use writable//file.setwritable (TRUE); //     /* //allow only file owners to use writable//file.setwritable (True, true); //      //allow only the file owner to use a readable//file.setreadable (True, true); *///  }      }            /*** View basic information for this file *@paramPath *@throwsIOException *@see[Class, Class # method, Class # member]*/       Public Static voidGetFileInfo (String Path)throwsIOException {File file=NewFile (path); //path to the fileString FilePath =File.getpath (); //the name of the fileString FileName =File.getname (); //File Parent PathString Fileparentname =file.getparent (); //absolute path to fileString Fileabsolutepath =File.getabsolutepath (); //absolute path to fileString Filecanonicalpath =File.getcanonicalpath (); //the file object of the parent directoryFile Fileparentfile =File.getparentfile (); //the file object of the parent directoryFile Fileabsolutefile =File.getabsolutefile (); //the file object of the parent directoryFile Filecanonicalfile =File.getcanonicalfile (); //available space for files        LongFilefreespace =File.getfreespace (); //total space for the file        LongFiletotalspace =File.gettotalspace (); //unused space for the file        LongFileusablespace =File.getusablespace (); System.out.println ("FilePath:" +FilePath); System.out.println ("FileName:" +fileName); System.out.println ("Fileparentname:" +fileparentname); System.out.println ("Fileabsolutepath:" +Fileabsolutepath); System.out.println ("Filecanonicalpath:" +Filecanonicalpath); System.out.println ("Fileparentfile:" +fileparentfile); System.out.println ("Fileabsolutefile:" +fileabsolutefile); System.out.println ("Filecanonicalfile:" +filecanonicalfile); System.out.println ("Filefreespace:" +filefreespace); System.out.println ("Filetotalspace:" +filetotalspace); System.out.println ("Fileusablespace:" +fileusablespace); /*result set: Filepath:d:\powerd\core.chm fileName:core.chm Fileparentname:d:\po Werd Fileabsolutepath:d:\powerd\core.chm Filecanonicalpath:d:\powerd\core.chm Fileparentfile:d:\po Werd Fileabsolutefile:d:\powerd\core.chm Filecanonicalfile:d:\powerd\core.chm fileFreeSpace:957297 90976 filetotalspace:158986403840 fileusablespace:95729790976*/        }            /*** Delete folder (delete files under file in, delete directory) *@paramargs *@see[Class, Class # method, Class # member]*/       Public Static voidDeleteFile (String path) {File file=NewFile (path); if(File.exists ()) {if(File.isfile ()) {System.out.println (File.getname () )+ "Whether the file is hidden:" +File.ishidden ());                 File.delete (); /*//Where the file path is checked for legality. Returns False when it is not valid. Legal, delete file, return True File.deleteonexit ();*/              }Else if(File.isdirectory ()) {file[] files=File.listfiles ();  for(File f:files) {DeleteFile (F.getabsolutepath ());      }}} file.delete (); }            /*** Rename *@paramOldPath *@paramNewPath *@see[Class, Class # method, Class # member]*/       Public Static voidrename (String oldpath,string newpath) {File oldFile=NewFile (OldPath); File NewFile=NewFile (NewPath); if(Oldfile.exists () &&!newfile.exists ()) {              if(!newfile.exists ()) {                    NewFile (Newfile.getparent ()). Mkdirs ();          } oldfile.renameto (NewFile); }      }            /*** Copy files *@paramOldPath *@paramNewPath *@throwsIOException *@see[Class, Class # method, Class # member]*/       Public Static voidCopyflie (String oldpath,string NewPath)throwsIOException {File oldFile=NewFile (OldPath); NewPath+="\\"+Oldfile.getname (); File NewFile=NewFile (NewPath); if(!newfile.exists ())            {Newfile.mkdirs ();            } System.out.println (NewFile); file[] File=Oldfile.listfiles ();  for(File f:file) {if(F.isfile ()) {String path2=newpath+ "\ \" +F.getname ();                CreateFile (path2); }Else if(F.isdirectory ()) {String s=F.getpath ();                Copyflie (S,newpath); }            }        }            /*** Do you have permission to file *@paramPath *@see[Class, Class # method, Class # member]*/       Public Static voidIsautho (String path) {File F=NewFile (path); if(!F.canexecute ()) {f.setexecutable (true); }          if(!F.canread ()) {f.setreadable (true); }          if(!F.canwrite ()) {f.setwritable (true); }     /*** Rename to remove suffix at end of file name *@paramOldPath *@paramNewPath *@see[Class, Class # method, Class # member]*/       Public Static voidrenamedir (String path,string suffix) {file file=NewFile (path); if(File.exists ()) {if(File.isfile ()) {if(File.getname (). EndsWith (suffix)) {String paths=File.getabsolutepath (); String Repath=paths.substring (0, Paths.length ()-suffix.length ());                  Rename (Paths,repath); }              }Else if(File.isdirectory ()) {file[] files=File.listfiles ();  for(File f:files) {Renamedir (F.getabsolutepath (), suffix); }              }          }     }  

Javafile Basic Operation

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.