程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java完成文件復制、剪切文件和刪除示例

java完成文件復制、剪切文件和刪除示例

編輯:關於JAVA

java完成文件復制、剪切文件和刪除示例。本站提示廣大學習愛好者:(java完成文件復制、剪切文件和刪除示例)文章只能為提供參考,不一定能成為您想要的結果。以下是java完成文件復制、剪切文件和刪除示例正文



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Java完成文件復制、剪切、刪除操作
 * 文件指文件或文件夾
 * 文件朋分符同一用"\\"
 */

public class FileOperateDemo {

    /**
     * 復制文件或文件夾
     * @param srcPath 源文件或源文件夾的途徑
     * @param destDir 目的文件地點的目次
     * @return
     */
    public static boolean copyGeneralFile(String srcPath, String destDir) {
        boolean flag = false;
        File file = new File(srcPath);
        if(!file.exists()) { // 源文件或源文件夾不存在
            return false;
        }

        if(file.isFile()) {    // 文件復制
            flag = copyFile(srcPath, destDir);
        }
        else if(file.isDirectory()) { // 文件夾復制
            flag = copyDirectory(srcPath, destDir);
        }

        return flag;
    }

    /**
     * 默許的復制文件辦法,默許會籠罩目的文件夾下的同名文件
     * @param srcPath
     *            源文件相對途徑
     * @param destDir
     *            目的文件地點目次
     * @return boolean
     */
    public static boolean copyFile(String srcPath, String destDir) {
     return copyFile(srcPath, destDir, true/**overwriteExistFile*/); // 默許籠罩同名文件
    }

    /**
     * 默許的復制文件夾辦法,默許會籠罩目的文件夾下的同名文件夾
     * @param srcPath    源文件夾途徑
     * @param destPath    目的文件夾地點目次
     * @return boolean
     */
    public static boolean copyDirectory(String srcPath, String destDir) {
     return copyDirectory(srcPath, destDir, true/**overwriteExistDir*/);
    }

    /**
     * 復制文件到目的目次
     * @param srcPath
     *            源文件相對途徑
     * @param destDir
     *            目的文件地點目次
     * @param overwriteExistFile
     *            能否籠罩目的目次下的同名文件
     * @return boolean
     */
    public static boolean copyFile(String srcPath, String destDir, boolean overwriteExistFile) {
        boolean flag = false;

        File srcFile = new File(srcPath);
        if (!srcFile.exists() || !srcFile.isFile()) { // 源文件不存在
            return false;
        }

        //獲得待復制文件的文件名
        String fileName = srcFile.getName();
        String destPath = destDir + File.separator +fileName;
        File destFile = new File(destPath);
        if (destFile.getAbsolutePath().equals(srcFile.getAbsolutePath())) { // 源文件途徑和目的文件途徑反復
            return false;
        }
        if(destFile.exists() && !overwriteExistFile) {    // 目的目次下已有同名文件且不許可籠罩
            return false;
        }

        File destFileDir = new File(destDir);
        if(!destFileDir.exists() && !destFileDir.mkdirs()) { // 目次不存在而且創立目次掉敗直接前往
         return false;
        }

        try {
            FileInputStream fis = new FileInputStream(srcPath);
            FileOutputStream fos = new FileOutputStream(destFile);
            byte[] buf = new byte[1024];
            int c;
            while ((c = fis.read(buf)) != -1) {
                fos.write(buf, 0, c);
            }
            fos.flush();
            fis.close();
            fos.close();

            flag = true;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return flag;
    }

    /**
     *
     * @param srcPath    源文件夾途徑
     * @param destPath    目的文件夾地點目次
     * @return
     */
    public static boolean copyDirectory(String srcPath, String destDir, boolean overwriteExistDir) {
        if(destDir.contains(srcPath))
           return false;

        boolean flag = false;

        File srcFile = new File(srcPath);
        if (!srcFile.exists() || !srcFile.isDirectory()) { // 源文件夾不存在
            return false;
        }

        //取得待復制的文件夾的名字,好比待復制的文件夾為"E:\\dir\\"則獲得的名字為"dir"
        String dirName = srcFile.getName();

        //目的文件夾的完全途徑
        String destDirPath = destDir + File.separator + dirName + File.separator;
        File destDirFile = new File(destDirPath);
        if(destDirFile.getAbsolutePath().equals(srcFile.getAbsolutePath())) {
         return false;
        }
        if(destDirFile.exists() && destDirFile.isDirectory() && !overwriteExistDir) {    // 目的地位有一個同名文件夾且不許可籠罩同名文件夾,則直接前往false
            return false;
        }

        if(!destDirFile.exists() && !destDirFile.mkdirs()) {  // 假如目的目次不存在而且創立目次掉敗
         return false;
        }

        File[] fileList = srcFile.listFiles();    //獲得源文件夾下的子文件和子文件夾
        if(fileList.length==0) {    // 假如源文件夾為空目次則直接設置flag為true,這一步異常隱藏,debug了良久
            flag = true;
        }
        else {
            for(File temp: fileList) {
                if(temp.isFile()) {    // 文件
                    flag = copyFile(temp.getAbsolutePath(), destDirPath, overwriteExistDir);     // 遞歸復制時也繼續籠罩屬性
                }
                else if(temp.isDirectory()) {    // 文件夾
                    flag = copyDirectory(temp.getAbsolutePath(), destDirPath, overwriteExistDir);   // 遞歸復制時也繼續籠罩屬性
                }

                if(!flag) {
                    break;
                }
            }
        }

        return flag;
    }

    /**
     * 刪除文件或文件夾
     * @param path
     *            待刪除的文件的相對途徑
     * @return boolean
     */
    public static boolean deleteFile(String path) {
        boolean flag = false;

        File file = new File(path);
        if (!file.exists()) { // 文件不存在直接前往
            return flag;
        }

        flag = file.delete();

        return flag;
    }

   
    /**
     * 由下面辦法延長出剪切辦法:復制+刪除
     * @param  destDir 同上
     */
    public static boolean cutGeneralFile(String srcPath, String destDir) {
     boolean flag = false;
        if(copyGeneralFile(srcPath, destDir) && deleteFile(srcPath)) { // 復制和刪除都勝利
         flag = true;
        }

        return flag;
    }

    public static void main(String[] args) {
     /** 測試復制文件 */
     System.out.println(copyGeneralFile("d://test/test.html", "d://test/test/"));  // 普通正常場景
     System.out.println(copyGeneralFile("d://notexistfile", "d://test/"));      // 復制不存在的文件或文件夾
     System.out.println(copyGeneralFile("d://test/test.html", "d://test/"));      // 待復制文件與目的文件在統一目次下
     System.out.println(copyGeneralFile("d://test/test.html", "d://test/test/"));  // 籠罩目的目次下的同名文件
     System.out.println(copyFile("d://test/", "d://test2", false)); // 不籠罩目的目次下的同名文件
     System.out.println(copyGeneralFile("d://test/test.html", "notexist://noexistdir/")); // 復制文件到一個弗成能存在也弗成能創立的目次下

     System.out.println("---------");

     /** 測試復制文件夾 */
     System.out.println(copyGeneralFile("d://test/", "d://test2/"));

     System.out.println("---------");

     /** 測試刪除文件 */
     System.out.println(deleteFile("d://a/"));
    }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved