Java file and folder copy, delete, move operations

Source: Internet
Author: User
Tags try catch
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.FileWriter;
Import Java.io.InputStream;

Import Java.io.PrintWriter; public class CopyFile {public CopyFile () {}/** * new directory * @param folderpath Strin  
           G such as C:/FQF * @return Boolean/public void NewFolder (String folderpath) {try {  
           String FilePath = FolderPath;  
           FilePath = Filepath.tostring ();  
           Java.io.File Myfilepath = new Java.io.File (FilePath);  
           if (!myfilepath.exists ()) {Myfilepath.mkdir ();  
           The catch (Exception e) {System.out.println ("Error in new directory operation");  
       E.printstacktrace (); }/** * New file * @param filepathandname String file path and name such as C:/fqf.txt * @param Filec Ontent String File Content * @return Boolean/public void NewFile (String filepathandname, String filecontent) {try {string filePath = Filepathand  
           Name;  FilePath = Filepath.tostring ();  
           Take the path and filename file Myfilepath = new file (FilePath);  
           /** Create a new file if the file does not exist * */if (!myfilepath.exists ()) {myfilepath.createnewfile ());  } FileWriter resultfile = new FileWriter (Myfilepath);  A convenient class for writing character files that constructs a FileWriter object printwriter myFile = new PrintWriter (resultfile) in the case of a file object;
           Prints a formatted representation of the object to the text output stream, using the specified file to create a new printwriter that does not have automatic row refreshes.  
           String strcontent = filecontent;  
           Myfile.println (strcontent);  
 
       Resultfile.close ();  
           catch (Exception e) {System.out.println ("New file Operation Error");  
 
       E.printstacktrace (); }/** * Delete file * @param filepathandname String file path and name such as C:/fqf.txt * @param fil EcontenT String * @return Boolean/public void Delfile (String filepathandname) {try {  
           String FilePath = filepathandname;  
           FilePath = Filepath.tostring ();  
           Java.io.File mydelfile = new Java.io.File (FilePath);  
 
       Mydelfile.delete ();  
           catch (Exception e) {System.out.println ("Error deleting file operation");  
 
       E.printstacktrace (); }/** * Delete folder * @param filepathandname String folder path and name such as C:/FQF * @param Filec  
           Ontent String * @return Boolean/public void Delfolder (string folderpath) {try {  Delallfile (FolderPath);  
           Delete all contents inside the String FilePath = FolderPath;  
           FilePath = Filepath.tostring ();  
           Java.io.File Myfilepath = new Java.io.File (FilePath);  Myfilepath.delete (); Delete Empty folders} catch (Exception e) {System.out.println ("Delete folder operation Error");  
 
       E.printstacktrace ();  }/** * Delete all files inside the folder * @param path String folder paths such as C:/FQF/public void  
       Delallfile (String path) {File File = new file (path);  
       if (!file.exists ()) {return;  
       } if (!file.isdirectory ()) {return;  
       } string[] Templist = File.list ();  
       File temp = null;  for (int i = 0;  i < templist.length;  
           i++) {if (Path.endswith (file.separator)) {temp = new File (path + templist[i]);  
           else {temp = new File (path + file.separator + templist[i]);  
           } if (Temp.isfile ()) {temp.delete ();  
       } if (Temp.isdirectory ()) {Delallfile (path+ "/" + templist[i])//delete files in folder first        Delfolder (path+ "/" + templist[i]);//delete Empty folder}}/** * Copy single file * @param oldpath String Original file path such as: C:/fqf.txt * @param newpath string Copy the path after: F:/fqf.txt * @return bo Olean */public void CopyFile (string oldpath, String NewPath) {try {//int byte  
           sum = 0;  
           int byteread = 0;  
           File Oldfile = new file (OldPath);  if (oldfile.exists ()) {//file exists InputStream instream = new FileInputStream (OldPath);  
               Read the original file FileOutputStream fs = new FileOutputStream (NewPath);  
byte[] buffer = new byte[1444];  
               int length;  while ((Byteread = instream.read (buffer))!=-1) {//Bytesum + = Byteread;  
                   Byte number file size//SYSTEM.OUT.PRINTLN (bytesum);  
      Fs.write (buffer, 0, byteread);         } instream.close ();  
           The catch (Exception e) {System.out.println ("error copying Single file operation");  
 
       E.printstacktrace (); }/** * Copy entire folder contents * @param oldpath String original file path such as: C:/FQF * @param newpath St Ring after replication path such as: F:/FQF/FF * @return Boolean */public void CopyFolder (string oldpath, String NEWP  Ath) {try {(new File (NewPath)). Mkdirs ();  
           If the folder does not exist, create a new folder, file A=new file (OldPath);  
           String[] File=a.list ();  
           File Temp=null;  for (int i = 0;  i < file.length;  
               i++) {if (Oldpath.endswith (File.separator)) {temp=new File (oldpath+file[i]);  
               } else{temp=new File (Oldpath+file.separator+file[i]);  
              } if (Temp.isfile ()) {     FileInputStream input = new FileInputStream (temp); FileOutputStream output = new FileOutputStream (NewPath + "/" + (Temp.getname ()). Tostrin  
                   g ());  
                   Byte[] B = New byte[1024 * 5];  
                   int Len;  
                   while (len = Input.read (b))!=-1) {output.write (b, 0, Len);  
                   } output.flush ();  
                   Output.close ();  
               Input.close (); } if (Temp.isdirectory ()) {//If it is a subfolder copyfolder (oldpath+ "/" +file[i],newpath+ "/" +file[i  
               ]);  
           (Exception e) {System.out.println ("Copy entire folder content operation error")  
 
       E.printstacktrace (); }/** * Move files to specified directory * @param oldpath String such as: C:/fqf.txt * @param newpath Strin G such as: D:/FQF. txt */public void MoveFile (string oldpath, String NewPath) {CopyFile (OldPath, NewPath);  
 
   Delfile (OldPath); /** * Move files to specified directory * @param oldpath string such as: C:/fqf.txt * @param newpath string such as: d:/f Qf.txt */public void MoveFolder (string oldpath, String NewPath) {CopyFolder (OldPath, NewPath  
       );  
 
   Delfolder (OldPath); 
    public static void Main (string[] args) {CopyFile file = new CopyFile ();//File.newfolder ("newFolder22222");
   File.delallfile ("E:/1");
   }//Copy file private void CopyFile2 (string source, String dest) {try {file in = new file (source);
   File out = new file (dest);
   FileInputStream inFile = new FileInputStream (in);
   FileOutputStream outfile = new FileOutputStream (out);
   byte[] buffer = new byte[10240];
   int i = 0;
   while ((i = infile.read (buffer))!=-1) {outfile.write (buffer, 0, I);
 }//end while Infile.close ();  Outfile.close (); }//end Try catch (Exception e) {}//end catch}//end CopyFile}

Related Article

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.