Java file Backup class

Source: Internet
Author: User

1 ImportJava.io.BufferedInputStream;2 ImportJava.io.BufferedOutputStream;3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 ImportJava.io.FileOutputStream;6 ImportJava.io.InputStream;7 ImportJava.io.OutputStream;8 9 /**Ten * Classes for file backup One  *  A * for various types of file backup, under the path of the original file, create a backup file, named the original file name. bak -  */ -  Public classFileUtils { the      Public StaticString backup_suffix = ". Bak"; -      -     /** - * functions to implement file copying +      *  - * Read and write files using binary stream format +      */ A      Public Static voidFileCopy (file srcfile, file destfile)throwsexception{ atInputStream src =NewBufferedinputstream (NewFileInputStream (srcfile)); -OutputStream dest =NewBufferedoutputstream (NewFileOutputStream (DestFile)); -          -         byte[] trans =New byte[1024]; -          -         intCount =-1; in          -          while((count = Src.read (trans))! =-1){ toDest.write (trans, 0, count); +         } -          the Dest.flush (); * src.close (); $ dest.close ();Panax Notoginseng     } -          the     /** + * Backup files, create backup files in the original file directory, name the original filename. bak A      * @paramtemplatefile functions that need to be backed up the      * @returntrue Success, False failed +      */ -      Public Static Booleanbackuptemplatefile (String templatefile) { $         BooleanFlag =true; $          -File Srcfile =NewFile (templatefile); -         if(!srcfile.exists ()) { theSystem.out.println ("Template file does not exist"); -             return false;Wuyi         } the          -         //Create a backup file WuFile BackUpFile =NewFile (templatefile+backup_suffix); -         Try { About             if(Backupfile.createnewfile ()) { $                 //Create backup file successfully, make file copy - fileCopy (Srcfile, backUpFile); -             } -}Catch(Exception e) { AFlag =false; +SYSTEM.OUT.PRINTLN ("Backup file Failed"); the         } -          $         returnFlag; the     } the}

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.