Application of operating the copy file of Java based on Java file

  • 2020-04-01 01:47:40
  • OfStack

Java file manipulation

package com.b510;

 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.InputStream;
 import java.io.PrintWriter;

 /**
  * 
  * @author Hongten</br>
  * 
  *          Operation of file 
  * 
  *         
  * 
  */
 public class OperateFiles {

     
     public static void main(String[] args) {
         OperateFiles operateFiles = new OperateFiles();
         //Create a new folder
         operateFiles.newFolder("c:/hongten");
         //Create a new file and write to it
         operateFiles.newFile("c:/hongten/Hello.txt", "hello,i'm Hongten. hello ");
         //Delete a file
         operateFiles.deleteFile("c:/hongten/Hello.txt");
         //Delete a file clip 
         operateFiles.deleteFolder("c:/hongten");
         //Copy folder
         operateFiles.copyFolder("c:/hongten", "e:/hongten");
         //Extract the file's extension
         String expandedName=operateFiles.getExpandedName("c:/hongten/Hello.txt");
         System.out.println(expandedName);
         //Extract the path to the file
         System.out.println(operateFiles.getFilePath("c:/hongten/Hello.txt"));
     }

     /**
      *  Gets the extension of the file 
      * @param filePath  Path to file   Such as: c:/hongten/Hello.txt
      * @return  The extension of the file   Such as: txt
 */
     public String getExpandedName(String filePath){
         return filePath.substring(filePath.lastIndexOf(".")+1);
     }
     
     public String getFilePath(String file){
         return file.substring(0,file.lastIndexOf("/"));  
     }
     
     public void newFolder(String folderPath) {
         try {
             File myFolderPath = new File(folderPath.toString());
             if (!myFolderPath.exists()) {
                 myFolderPath.mkdir();
             }
         } catch (Exception e) {
             System.out.println(" Error creating new directory ");
             e.printStackTrace();
         }
     }

     
     public void newFile(String filePath) {
         try {
             File myFilePathFile = new File(filePath.toString());
             if (!myFilePathFile.exists()) {
                 myFilePathFile.createNewFile();
             }
         } catch (Exception e) {
             System.out.println(" New file creation failed ");
             e.printStackTrace();
         }
     }

     
     public void newFile(String filePath, String fileContent) {
         try {
             newFile(filePath);
             FileWriter resultFile = new FileWriter(filePath);
             PrintWriter myFile = new PrintWriter(resultFile);
             myFile.println(fileContent);
             resultFile.close();
         } catch (Exception e) {
             System.out.println(" Error creating new file ");
             e.printStackTrace();
         }
     }

     
     public void deleteFile(String filePath) {
         try {
             File preDelFile = new File(filePath);
             if (preDelFile.exists()) {
                 preDelFile.delete();
             } else {
                 System.out.println(filePath + " Does not exist! ");
             }
         } catch (Exception e) {
             System.out.println(" Error deleting file ");
             e.printStackTrace();
         }
     }

     
     public void deleteFolder(String folderPath) {
         try {
             delAllFiles(folderPath);
             File preDelFoder = new File(folderPath);
             if (preDelFoder.exists()) {
                 preDelFoder.delete();
             } else {
                 System.out.println(folderPath + " Does not exist! ");
             }
         } catch (Exception e) {
             System.out.println(" Error deleting file ");
             e.printStackTrace();
         }
     }

     
     public void delAllFiles(String folderPath) {
         File file = new File(folderPath);
         if (!file.exists()) {
             return;
         }
         if (!file.isDirectory()) {
             return;
         }
         String[] tempList = file.list();
         File temp = null;
         for (int i = 0; i < tempList.length; i++) {
             if (folderPath.endsWith(File.separator)) {
                 temp = new File(folderPath + tempList[i]);
             } else {
                 temp = new File(folderPath + File.separator + tempList[i]);
             }
             if (temp.isFile()) {
                 temp.delete();
             }
             if (temp.isDirectory()) {
                 delAllFiles(folderPath + "/" + tempList[i]);//Delete the files in the folder first
                 deleteFolder(folderPath + "/" + tempList[i]);//Delete the empty folder
             }
         }
     }

     
     public void copyFile(String oldPath, String newPath) {
         try {
             int bytesum = 0;
             int byteread = 0;
             File oldfile = new File(oldPath);
             if (oldfile.exists()) { //File existence
                 InputStream inStream = new FileInputStream(oldPath); //Read in the original file
                 FileOutputStream fs = new FileOutputStream(newPath);
                 byte[] buffer = new byte[1444];
                 // int length = 0;
                 while ((byteread = inStream.read(buffer)) != -1) {
                     bytesum += byteread; //Number of bytes file size
                     fs.write(buffer, 0, byteread);
                 }
                 inStream.close();
             }
         } catch (Exception e) {
             System.out.println(" There was an error copying a single file ");
             e.printStackTrace();

         }
     }

     
     public void copyFolder(String oldPath, String newPath) {

         try {
             (new File(newPath)).mkdirs(); //Create a new folder if the folder does not exist
             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()).toString());
                     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's a subfolder
                     copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
                 }
             }
         } catch (Exception e) {
             System.out.println(" Error copying entire folder contents ");
             e.printStackTrace();

         }
     }

     
     public void moveFile(String oldPath, String newPath) {
         copyFile(oldPath, newPath);
         deleteFile(oldPath);
     }

     
     public void moveFolder(String oldPath, String newPath) {
         copyFolder(oldPath, newPath);
         deleteFolder(oldPath);
     }

     
     public String getPath() {
         String sysPath = this.getClass().getResource("/").getPath();
         //Modify the path
         sysPath = sysPath.substring(1, sysPath.length() - 16);
         return sysPath;
     }

 }

Now I have time to put these things together and share them with you...

Related articles: