Java Create, rename, delete files and folders

Source: Internet
Author: User
Tags log log

Java file operation is too basic, lack of many practical tools, such as the operation of the directory, support is very poor. If you often use Java to manipulate files or folders, you will find it frustrating to write the code over and over again, and use recursion a lot.

The following is a solution that uses the Apache Commons IO Toolkit (COMMONS-IO-1.1.JAR) to make it easy to copy, move, delete, and size files (folders).

Importorg.apache.commons.io.FileUtils;ImportOrg.apache.commons.io.filefilter.*; ImportOrg.apache.commons.logging.Log;Importorg.apache.commons.logging.LogFactory;ImportJava.io.*;/*** File Toolbox * *@authorleizhimin 2008-12-15 13:59:16*/  Public Final classFiletoolkit {Private Static FinalLog log = Logfactory.getlog (Filetoolkit.class); /*** Copy files or directories, copy and post files exactly the same. *          * @paramResfilepath Source file path *@paramdistfolder Target Folder * @IOException thrown when an operation exception occurs*/          Public Static voidCopyFile (String Resfilepath, String distfolder)throwsIOException {File resfile=NewFile (Resfilepath); File Distfile=NewFile (Distfolder); if(Resfile.isdirectory ()) {fileutils.copydirectorytodirectory (resfile, distfile); } Else if(Resfile.isfile ()) {fileutils.copyfiletodirectory (Resfile, Distfile,true); }         }        /*** Delete a file or directory * *@paramtargetpath file or directory path * @IOException thrown when an operation exception occurs*/          Public Static voidDeleteFile (String TargetPath)throwsIOException {File targetfile=NewFile (TargetPath); if(Targetfile.isdirectory ()) {fileutils.deletedirectory (targetfile); } Else if(Targetfile.isfile ()) {targetfile.delete (); }         }        /*** Move files or directories, move files exactly like before and after, if the destination folder does not exist then create. *          * @paramResfilepath Source file path *@paramdistfolder Target Folder * @IOException thrown when an operation exception occurs*/          Public Static voidMoveFile (String Resfilepath, String distfolder)throwsIOException {File resfile=NewFile (Resfilepath); File Distfile=NewFile (Distfolder); if(Resfile.isdirectory ()) {fileutils.movedirectorytodirectory (Resfile, Distfile,true); } Else if(Resfile.isfile ()) {fileutils.movefiletodirectory (Resfile, Distfile,true); }         }        /*** Rename file or folder * *@paramResfilepath Source file path *@paramNewFileName Rename *@returnOperation Success Identification*/          Public Static Booleanrenamefile (String resfilepath, String newfilename) {string Newfilepath= Stringtoolkit.formatpath (Stringtoolkit.getparentpath (Resfilepath) + "/" +newfilename); File Resfile=NewFile (Resfilepath); File NewFile=NewFile (Newfilepath); returnResfile.renameto (NewFile); }        /*** Read the size of the file or directory * *@paramdistfilepath target file or folder *@returnthe size of the file or directory, and returns 1 if the fetch fails*/          Public Static Longgenfilesize (String distfilepath) {File distfile=NewFile (Distfilepath); if(Distfile.isfile ()) {returndistfile.length (); } Else if(Distfile.isdirectory ()) {returnfileutils.sizeofdirectory (Distfile); }                 return-1l; }        /*** Determine if a file exists * *@paramfilePath File path *@returnThe presence returns TRUE, otherwise returns false*/          Public Static Booleanisexist (String filePath) {return NewFile (FilePath). exists (); }        /*** List of files in a local directory (not recursive) *@parama directory on the folder FTP *@paramthe suffix name of the suffix file (e.g.. mov.xml) *@returnList of file names*/          Public Staticstring[] Listfilebysuffix (string folder, string suffix) {iofilefilter fileFilter1=Newsuffixfilefilter (suffix); Iofilefilter FileFilter2=NewNotfilefilter (directoryfilefilter.instance); FilenameFilter FilenameFilter=NewAndfilefilter (FileFilter1, FileFilter2); return NewFile (folder). List (filenamefilter); }        /*** Writes a string to the specified file (when the folder does not exist in the specified parent path, it is created to the maximum extent possible to ensure the success of the Save!) )          *          * @paramRes Original String *@paramfilePath File path *@returnSuccess Token*/          Public Static BooleanString2file (String res, string filePath) {BooleanFlag =true; BufferedReader BufferedReader=NULL; BufferedWriter BufferedWriter=NULL; Try{File Distfile=NewFile (FilePath); if(!Distfile.getparentfile (). exists ()) Distfile.getparentfile (). Mkdirs (); BufferedReader=NewBufferedReader (NewStringReader (res)); BufferedWriter=NewBufferedWriter (NewFileWriter (distfile)); CharBuf[] =New Char[1024];//character Buffers                        intLen;  while(len = Bufferedreader.read (BUF))! =-1) {bufferedwriter.write (buf,0, Len);                         } bufferedwriter.flush ();                         Bufferedreader.close ();                 Bufferedwriter.close (); } Catch(IOException e) {flag=false;                 E.printstacktrace (); }                 returnFlag; } }

http://xiaoxuejie.iteye.com/blog/1325331

Java Create, rename, delete files and folders (GO)

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.