<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);// 調用方法實現目錄以用目錄下的文件和子目錄全部復制
    }
    }

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


    網站導航:
     
    主站蜘蛛池模板: 亚洲av产在线精品亚洲第一站| 国产AV无码专区亚洲AVJULIA | 中文字幕在线观看亚洲日韩| 98精品全国免费观看视频| 亚洲国产精品无码专区在线观看 | ww在线观视频免费观看| 亚洲色图综合网站| www.免费在线观看| 日韩亚洲国产高清免费视频| 青青草免费在线视频| 亚洲AV无码一区二区一二区| 日本不卡在线观看免费v| 免费VA在线观看无码| 国产亚洲精aa成人网站| 免费人成网站在线观看不卡| 亚洲激情视频在线观看| 国产jizzjizz免费看jizz| 亚洲另类激情综合偷自拍图| 中文字幕永久免费视频| 亚洲视频在线视频| 在线永久免费的视频草莓| 精品亚洲国产成人| 免费观看亚洲人成网站| 国产午夜精品理论片免费观看| 亚洲成A人片在线观看无码不卡| 最近免费中文字幕大全免费 | 亚洲精品美女久久久久久久| 又黄又爽一线毛片免费观看| 亚洲免费日韩无码系列| 7777久久亚洲中文字幕蜜桃| 久久这里只有精品国产免费10| 亚洲AV一区二区三区四区| 在线精品亚洲一区二区三区| 24小时日本韩国高清免费| 亚洲国产成人久久精品大牛影视 | 1a级毛片免费观看| 亚洲а∨精品天堂在线| 亚洲欧洲无码AV电影在线观看| 亚洲国产精品免费在线观看| 精品久久久久久久久亚洲偷窥女厕| 亚洲熟妇无码乱子AV电影|