<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我的家園

    我的家園

    JAVA 文件復制,移動工具(實用)

    Posted on 2012-04-15 16:37 zljpp 閱讀(250) 評論(0)  編輯  收藏

    package com.file;

    import java.io.File;//引入類
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;

    //實現文件的簡單處理,復制和移動文件、目錄等
    public class TextCopyFileAndMove {
    public static void fileMove(String from, String to) throws Exception {// 移動指定文件夾內的全部文件
    try {
    File dir = new File(from);
    File[] files = dir.listFiles();// 將文件或文件夾放入文件集
    if (files == null)// 判斷文件集是否為空
    return;
    File moveDir = new File(to);// 創建目標目錄
    if (!moveDir.exists()) {// 判斷目標目錄是否存在
    moveDir.mkdirs();// 不存在則創建
    }
    for (int i = 0; i < files.length; i++) {// 遍歷文件集
    if (files[i].isDirectory()) {// 如果是文件夾或目錄,則遞歸調用fileMove方法,直到獲得目錄下的文件
    fileMove(files[i].getPath(), to + "\\" + files[i].getName());// 遞歸移動文件
    files[i].delete();// 刪除文件所在原目錄
    }
    File moveFile = new File(moveDir.getPath() + "\\"http:// 將文件目錄放入移動后的目錄
    + files[i].getName());
    if (moveFile.exists()) {// 目標文件夾下存在的話,刪除
    moveFile.delete();
    }
    files[i].renameTo(moveFile);// 移動文件
    System.out.println(files[i] + " 移動成功");
    }
    } catch (Exception e) {
    throw e;
    }
    }

    // 復制目錄下的文件(不包括該目錄)到指定目錄,會連同子目錄一起復制過去。
    public static void copyFileFromDir(String toPath, String fromPath) {
    File file = new File(fromPath);
    createFile(toPath, false);// true:創建文件 false創建目錄
    if (file.isDirectory()) {// 如果是目錄
    copyFileToDir(toPath, listFile(file));
    }
    }

    // 復制目錄到指定目錄,將目錄以及目錄下的文件和子目錄全部復制到目標目錄
    public static void copyDir(String toPath, String fromPath) {
    File targetFile = new File(toPath);// 創建文件
    createFile(targetFile, false);// 創建目錄
    File file = new File(fromPath);// 創建文件
    if (targetFile.isDirectory() && file.isDirectory()) {// 如果傳入是目錄
    copyFileToDir(targetFile.getAbsolutePath() + "/" + file.getName(),
    listFile(file));// 復制文件到指定目錄
    }
    }

    // 復制一組文件到指定目錄。targetDir是目標目錄,filePath是需要復制的文件路徑
    public static void copyFileToDir(String toDir, String[] filePath) {
    if (toDir == null || "".equals(toDir)) {// 目錄路徑為空
    System.out.println("參數錯誤,目標路徑不能為空");
    return;
    }
    File targetFile = new File(toDir);
    if (!targetFile.exists()) {// 如果指定目錄不存在
    targetFile.mkdir();// 新建目錄
    } else {
    if (!targetFile.isDirectory()) {// 如果不是目錄
    System.out.println("參數錯誤,目標路徑指向的不是一個目錄!");
    return;
    }
    }
    for (int i = 0; i < filePath.length; i++) {// 遍歷需要復制的文件路徑
    File file = new File(filePath[i]);// 創建文件
    if (file.isDirectory()) {// 判斷是否是目錄
    copyFileToDir(toDir + "/" + file.getName(), listFile(file));// 遞歸調用方法獲得目錄下的文件
    System.out.println("復制文件 " + file);
    } else {
    copyFileToDir(toDir, file, "");// 復制文件到指定目錄
    }
    }
    }

    public static void copyFileToDir(String toDir, File file, String newName) {// 復制文件到指定目錄
    String newFile = "";
    if (newName != null && !"".equals(newName)) {
    newFile = toDir + "/" + newName;
    } else {
    newFile = toDir + "/" + file.getName();
    }
    File tFile = new File(newFile);
    copyFile(tFile, file);// 調用方法復制文件
    }

    public static void copyFile(File toFile, File fromFile) {// 復制文件
    if (toFile.exists()) {// 判斷目標目錄中文件是否存在
    System.out.println("文件" + toFile.getAbsolutePath() + "已經存在,跳過該文件!");
    return;
    } else {
    createFile(toFile, true);// 創建文件
    }
    System.out.println("復制文件" + fromFile.getAbsolutePath() + "到"
    + toFile.getAbsolutePath());
    try {
    InputStream is = new FileInputStream(fromFile);// 創建文件輸入流
    FileOutputStream fos = new FileOutputStream(toFile);// 文件輸出流
    byte[] buffer = new byte[1024];// 字節數組
    while (is.read(buffer) != -1) {// 將文件內容寫到文件中
    fos.write(buffer);
    }
    is.close();// 輸入流關閉
    fos.close();// 輸出流關閉
    } catch (FileNotFoundException e) {// 捕獲文件不存在異常
    e.printStackTrace();
    } catch (IOException e) {// 捕獲異常
    e.printStackTrace();
    }
    }

    public static String[] listFile(File dir) {// 獲取文件絕對路徑
    String absolutPath = dir.getAbsolutePath();// 聲獲字符串賦值為路傳入文件的路徑
    String[] paths = dir.list();// 文件名數組
    String[] files = new String[paths.length];// 聲明字符串數組,長度為傳入文件的個數
    for (int i = 0; i < paths.length; i++) {// 遍歷顯示文件絕對路徑
    files[i] = absolutPath + "/" + paths[i];
    }
    return files;
    }

    public static void createFile(String path, boolean isFile) {// 創建文件或目錄
    createFile(new File(path), isFile);// 調用方法創建新文件或目錄
    }

    public static void createFile(File file, boolean isFile) {// 創建文件
    if (!file.exists()) {// 如果文件不存在
    if (!file.getParentFile().exists()) {// 如果文件父目錄不存在
    createFile(file.getParentFile(), false);
    } else {// 存在文件父目錄
    if (isFile) {// 創建文件
    try {
    file.createNewFile();// 創建新文件
    } catch (IOException e) {
    e.printStackTrace();
    }
    } else {
    file.mkdir();// 創建目錄
    }
    }
    }
    }

    public static void main(String[] args) {// java程序主入口處
    String fromPath = "E:/createFile";// 目錄路徑
    String toPath = "F:/createFile";// 源路徑
    System.out.println("1.移動文件:從路徑 " + fromPath + " 移動到路徑 " + toPath);
    try {
    fileMove(fromPath, toPath);// 調用方法實現文件的移動
    } catch (Exception e) {
    System.out.println("移動文件出現問題" + e.getMessage());
    }
    System.out.println("2.復制目錄 " + toPath + " 下的文件(不包括該目錄)到指定目錄" + fromPath
    + " ,會連同子目錄一起復制過去。");
    copyFileFromDir(fromPath, toPath);// 調用方法實現目錄復制
    System.out.println("3.復制目錄 " + fromPath + "到指定目錄 " + toPath
    + " ,將目錄以及目錄下的文件和子目錄全部復制到目標目錄");
    copyDir(toPath, fromPath);// 調用方法實現目錄以用目錄下的文件和子目錄全部復制
    }
    }

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲精品无码永久在线观看你懂的 | 四虎影视在线永久免费看黄| 亚洲一区二区三区在线网站| 免费人成视频在线观看网站| 亚洲自偷自拍另类12p| 120秒男女动态视频免费| 亚洲乱亚洲乱淫久久| 五月亭亭免费高清在线| 激情五月亚洲色图| 国产精品无码一区二区三区免费| 亚洲av无码偷拍在线观看| 亚洲AV网站在线观看| 国产性生大片免费观看性| 亚洲AV日韩AV天堂久久| 69pao强力打造免费高清| 亚洲综合一区无码精品| 四虎影永久在线高清免费| 中文字幕看片在线a免费| 亚洲欧洲日产国码久在线观看| 在线a免费观看最新网站| 亚洲综合丁香婷婷六月香| 国产成人精品123区免费视频| 白白色免费在线视频| 亚洲伊人久久大香线蕉综合图片| 99国产精品免费观看视频| 亚洲av无码片区一区二区三区| 国产美女无遮挡免费网站| 中文在线免费观看| 亚洲国产成人精品久久| 国产成人aaa在线视频免费观看 | a色毛片免费视频| 日韩精品亚洲人成在线观看 | 免费观看美女裸体网站| 色老头综合免费视频| 久久亚洲私人国产精品vA | 波多野结衣免费在线观看| 国产偷国产偷亚洲高清人| 亚洲产国偷V产偷V自拍色戒 | 中文字幕永久免费视频| 国产成人精品日本亚洲11| 亚洲精品成人网久久久久久|