zoukankan      html  css  js  c++  java
  • Java 文件及文件夹复制

    // 调用复制文件夹和文件的方法 

    public static void main(String[] args) {

    try {
    long startTime = System.currentTimeMillis();// 记录开始时间
    copyDir("F://fromFile", "E://toFile"); // 文件夾複製
    copyFileUsingFileStreams("E://fromFile.doc", "F://toFile.doc"); // 文件複製
    long endTime = System.currentTimeMillis();// 记录结束时间
    float excTime = (float) (endTime - startTime) / 1000;
    System.out.println("执行时间:" + excTime + "s"); // 輸出用時
    }
    catch (IOException e) {
    System.out.println("失敗");
    e.printStackTrace();
    }
    }
     

    // 複製文件

    private static void copyFileUsingFileStreams(String source, String dest) throws IOException {
    InputStream input = null;
    OutputStream output = null;
    try {
    input = new FileInputStream(new File(source));
    output = new FileOutputStream(new File(dest));
    byte[] buf = new byte[1024];
    int bytesRead;
    while ((bytesRead = input.read(buf)) != -1) {
    output.write(buf, 0, bytesRead);
    }
    }
    finally {
    input.close();
    output.close();
    }
    }
    // 複製文件夾
    private static void copyDir(String sourcePath, String newPath) throws IOException {
    File file = new File(sourcePath);
    String[] filePath = file.list();
    if (!(new File(newPath)).exists()) {
    new File(newPath)).mkdir();
    }
    for (int i = 0; i < filePath.length; i++) {
    if ((new File(sourcePath + file.separator + filePath[i])).isDirectory()) {
    copyDir(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
    }
    if (new File(sourcePath + file.separator + filePath[i]).isFile()) {
    copyFileUsingFileStreams(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
    }
    }
    }
  • 相关阅读:
    Android桌面快捷方式的实现
    Java之currenHashMap
    windows 2003 配置邮件服务器 hMailServer+squirrelmail
    bat脚本 cmd 命令
    强制客户端更新Silverlight XAP文件方法汇总(转)
    Silverlight 常用StringFormat格式总结(转)
    Excel 操作 DLL
    silverlight 开发软件列表
    通用错误处理
    use XSD.exe in VS2010 from a xsd file to class
  • 原文地址:https://www.cnblogs.com/zhangzack/p/9511143.html
Copyright © 2011-2022 走看看