Java implementation Simple File Manipulation tool class

Source: Internet
Author: User
Tags file copy

This article introduces a simple tool class for file operations, including folder creation, folder deletion, file creation, file renaming, file copying, and file Deletion. If you need to copy a folder, you are actually creating the folder and copying the Files. Directly below the code

 packagecom.util;Importjava.io.BufferedInputStream;Importjava.io.BufferedOutputStream;Importjava.io.File;Importjava.io.FileInputStream;Importjava.io.FileOutputStream;Importjava.io.IOException;/*** File Manipulation tool class, providing the functions of folder creation, folder deletion, file creation, file deletion, file renaming, file copying, etc.*/ public classFILECONTROLUNITL {/*** Create folder *@paramURL created folder path, for example: e:\\workspaces\\myeclipse 10\\test\\src\\com\\file, where file is the name of the folder that will be created *@returnBoolean returns the creation succeeded (true) or failed (false) *@throwsException*/     public Static BooleanCreateFolder (String Url)throwsException{url= Url.replace ("\ \", "/"); File folder=NewFile (url); if(folder.isdirectory () | |folder.exists ()) {            Throw NewIOException ("failed to create folder: [" +folder+ "] folder already exists!) "); }        if(!Folder.isfile ())        {folder.mkdirs (); }        //detect if a folder was created successfully        if(folder.isdirectory () &&folder.exists ()) {            return true; }Else {            return false; }    }        /*** Delete folder based on folder name and path *@paramURL file and its path, for example: e:\\workspaces\\myeclipse 10\\test\\src\\com\\file *@return     * @throwsIOException*/     public Static BooleanDeleteFolder (String Url)throwsIoexception{url= Url.replace ("\ \", "/"); File folder=NewFile (url); //determine if the directory        if(!folder.isdirectory ()) {            Throw NewIOException ("folder deletion failed: [" +folder+ "] is not a directory! "); }        if(!folder.exists ()) {            Throw NewIOException ("folder deletion failed: [" +folder+ "] folder does not exist!) "); }        //determine if a file exists, file is not allowed to delete        if(folder.listfiles (). length>0){            Throw NewIOException ("folder deletion failed: file or folder exists under [" +folder+ "] directory!) "); }        //Delete a folderFolder.Delete (); //returns whether folder deletion was successful        if(folder.exists ()) {return false; }Else{            return true; }    }        /*** Create files based on path and file name *@paramURL to create the path to the file, for example: e:\\workspaces\\myeclipse 10\\test\\src\\com\\file *@paramname File name for example: Testfile.java *@returnBoolean returns the creation succeeded (true) or failed (false) *@throwsException*/     public Static BooleanCreateFile (String url,string Name)throwsException{url= Url.replace ("\ \", "/"); File folder=NewFile (url); //determine if it is a directory        if(!folder.isdirectory ()) {            Throw NewIOException ("failed to create file: [" +folder+ "] is not a folder path! "); }        //determine if the file already existsurl = url + "/" +name; File File=NewFile (url); if(file.exists ()) {Throw NewIOException ("failed to create file: [" +name+ "] file already exists!) "); }        //Create a fileFile.createnewfile (); //detects if the file was created successfully        if(file.exists () &&File.isfile ()) {            return true; }Else {            return false; }    }    /*** Rename filename according to old file address and file name *@paramURL old file address and file name, such as: e:\\workspaces\\myeclipse 10\\test\\src\\com\\file\\testfile.java *@paramnewname New File name: New_testfile.java *@return     * @throwsIOException*/     public Static BooleanReanmefile (String url,string Newname)throwsioexception{String Old_url= Url.replace ("\ \", "/"); File Old_file=NewFile (old_url); if(!old_file.isfile () | |!old_file.exists ()) {            Throw NewIOException ("file rename failed: [" +old_file+ "] file does not exist!) "); }        //get the old file nameString Old_name =Old_file.getname (); //Get parent file pathString Parent_url =old_file.getparent (); // renamingString New_url = parent_url + "/" +newname; File New_file=NewFile (new_url);        Old_file.renameto (new_file); //Verify that the file is renamed successfullyString new_name =New_file.getname (); if(old_name.equals (new_name)) {return false; }Else{            return true; }    }        /*** Copy files according to source file and copy path *@paramsourceURL source files and their paths such as: e:\\workspaces\\myeclipse 10\\test\\src\\com\\file\\testfile.java *@paramcopyurl Copy file path such as: e:\\workspaces\\myeclipse 10\\test\\src\\com\\file\\copyfile *@return     * @throwsException*/     public Static BooleanCopyFile (String sourceurl,string Copyurl)throwsException{sourceurl= Sourceurl.replace ("\ \", "/"); Copyurl= Copyurl.replace ("\ \", "/"); File source_file=NewFile (sourceurl); File Copy_file=NewFile (copyurl); //determine if the file and the replication path exist        if(!source_file.exists ()) {            Throw NewIOException ("file copy failed: source file [" +source_file+ "] does not exist"); }        if(!copy_file.isdirectory ()) {            Throw NewIOException ("file copy failed: copy path [" +copyurl+ "] error"); }//File parent = copy_file.getparentfile ();//        //Create a replication path//if (!parent.exists ()) {//parent.mkdirs ();//        }        //Create a copy fileCopyurl = Copyurl + "/" +Source_file.getname (); Copy_file=NewFile (copyurl); if(!copy_file.exists ())        {copy_file.createnewfile (); } FileInputStream FIS=NewFileInputStream (source_file); FileOutputStream Fos=NewFileOutputStream (copy_file); Bufferedinputstream bis=NewBufferedinputstream (fis); Bufferedoutputstream Bos=NewBufferedoutputstream (fos); byte[] KB =New byte[1024]; intindex;  while(index = Bis.read (kb))!=-1) {bos.write (kb,0, index);        } bos.close ();        Bis.close ();        Fos.close ();        Fis.close (); //determine if the file was copied successfully        if(!copy_file.exists ()) {            return false; }Else if(source_file.length ()! =Copy_file.length ()) {            return false; }Else{            return true; }    }        /*** Delete files based on file and path *@paramURL file and its path, for example: e:\\workspaces\\myeclipse 10\\test\\src\\com\\file\\testfile.java *@return     * @throwsIOException*/     public Static BooleanDeleteFile (String Url)throwsIoexception{url= Url.replace ("\ \", "/"); File File=NewFile (url); //determine if the file is        if(!File.isfile ()) {            Throw NewIOException ("file deletion failed: [" +file+ "] is not a file! "); }        //determine if a file exists        if(!file.exists ()) {            Throw NewIOException ("file deletion failed: [" +file+ "] file does not exist!) "); }        //Deleting FilesFile.delete (); //returns whether the file was deleted successfully        if(file.exists ()) {return false; }Else{            return true; }    }    }

The general process of file operation is this way, as to the details, the form of parameters, check, return type and other issues can be adjusted according to the needs of the Perfect.

Do not like to spray, come, we together heaven

Java implementation Simple File Manipulation tool class

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.