AAA java实现文件的复制,移动

来源:互联网 发布:pubmed数据库电脑官网 编辑:程序博客网 时间:2024/06/05 15:00
  1. java实现文件的复制,移动  
  2.   
  3. package com.file;  
  4.   
  5. import java.io.File;//引入类  
  6. import java.io.FileInputStream;  
  7. import java.io.FileNotFoundException;  
  8. import java.io.FileOutputStream;  
  9. import java.io.IOException;  
  10. import java.io.InputStream;  
  11.   
  12. //实现文件的简单处理,复制和移动文件、目录等  
  13. public class TextCopyFileAndMove {  
  14.     public static void fileMove(String from, String to) throws Exception {// 移动指定文件夹内的全部文件  
  15.         try {  
  16.             File dir = new File(from);  
  17.             File[] files = dir.listFiles();// 将文件或文件夹放入文件集  
  18.             if (files == null)// 判断文件集是否为空  
  19.                 return;  
  20.             File moveDir = new File(to);// 创建目标目录  
  21.             if (!moveDir.exists()) {// 判断目标目录是否存在  
  22.                 moveDir.mkdirs();// 不存在则创建  
  23.             }  
  24.             for (int i = 0; i < files.length; i++) {// 遍历文件集  
  25.                 if (files[i].isDirectory()) {// 如果是文件夹或目录,则递归调用fileMove方法,直到获得目录下的文件  
  26.                     fileMove(files[i].getPath(), to + "\\" + files[i].getName());// 递归移动文件  
  27.                     files[i].delete();// 删除文件所在原目录  
  28.                 }  
  29.                 File moveFile = new File(moveDir.getPath() + "\\"// 将文件目录放入移动后的目录  
  30.                         + files[i].getName());  
  31.                 if (moveFile.exists()) {// 目标文件夹下存在的话,删除  
  32.                     moveFile.delete();  
  33.                 }  
  34.                 files[i].renameTo(moveFile);// 移动文件  
  35.                 System.out.println(files[i] + " 移动成功");  
  36.             }  
  37.         } catch (Exception e) {  
  38.             throw e;  
  39.         }  
  40.     }  
  41.   
  42.     // 复制目录下的文件(不包括该目录)到指定目录,会连同子目录一起复制过去。  
  43.     public static void copyFileFromDir(String toPath, String fromPath) {  
  44.         File file = new File(fromPath);  
  45.         createFile(toPath, false);// true:创建文件 false创建目录  
  46.         if (file.isDirectory()) {// 如果是目录  
  47.             copyFileToDir(toPath, listFile(file));  
  48.         }  
  49.     }  
  50.   
  51.     // 复制目录到指定目录,将目录以及目录下的文件和子目录全部复制到目标目录  
  52.     public static void copyDir(String toPath, String fromPath) {  
  53.         File targetFile = new File(toPath);// 创建文件  
  54.         createFile(targetFile, false);// 创建目录  
  55.         File file = new File(fromPath);// 创建文件  
  56.         if (targetFile.isDirectory() && file.isDirectory()) {// 如果传入是目录  
  57.             copyFileToDir(targetFile.getAbsolutePath() + "/" + file.getName(),  
  58.                     listFile(file));// 复制文件到指定目录  
  59.         }  
  60.     }  
  61.   
  62.     // 复制一组文件到指定目录。targetDir是目标目录,filePath是需要复制的文件路径  
  63.     public static void copyFileToDir(String toDir, String[] filePath) {  
  64.         if (toDir == null || "".equals(toDir)) {// 目录路径为空  
  65.             System.out.println("参数错误,目标路径不能为空");  
  66.             return;  
  67.         }  
  68.         File targetFile = new File(toDir);  
  69.         if (!targetFile.exists()) {// 如果指定目录不存在  
  70.             targetFile.mkdir();// 新建目录  
  71.         } else {  
  72.             if (!targetFile.isDirectory()) {// 如果不是目录  
  73.                 System.out.println("参数错误,目标路径指向的不是一个目录!");  
  74.                 return;  
  75.             }  
  76.         }  
  77.         for (int i = 0; i < filePath.length; i++) {// 遍历需要复制的文件路径  
  78.             File file = new File(filePath[i]);// 创建文件  
  79.             if (file.isDirectory()) {// 判断是否是目录  
  80.                 copyFileToDir(toDir + "/" + file.getName(), listFile(file));// 递归调用方法获得目录下的文件  
  81.                 System.out.println("复制文件 " + file);  
  82.             } else {  
  83.                 copyFileToDir(toDir, file, "");// 复制文件到指定目录  
  84.             }  
  85.         }  
  86.     }  
  87.   
  88.     public static void copyFileToDir(String toDir, File file, String newName) {// 复制文件到指定目录  
  89.         String newFile = "";  
  90.         if (newName != null && !"".equals(newName)) {  
  91.             newFile = toDir + "/" + newName;  
  92.         } else {  
  93.             newFile = toDir + "/" + file.getName();  
  94.         }  
  95.         File tFile = new File(newFile);  
  96.         copyFile(tFile, file);// 调用方法复制文件  
  97.     }  
  98.   
  99.     public static void copyFile(File toFile, File fromFile) {// 复制文件  
  100.         if (toFile.exists()) {// 判断目标目录中文件是否存在  
  101.             System.out.println("文件" + toFile.getAbsolutePath() + "已经存在,跳过该文件!");  
  102.             return;  
  103.         } else {  
  104.             createFile(toFile, true);// 创建文件  
  105.         }  
  106.         System.out.println("复制文件" + fromFile.getAbsolutePath() + "到"  
  107.                 + toFile.getAbsolutePath());  
  108.         try {  
  109.             InputStream is = new FileInputStream(fromFile);// 创建文件输入流  
  110.             FileOutputStream fos = new FileOutputStream(toFile);// 文件输出流  
  111.             byte[] buffer = new byte[1024];// 字节数组  
  112.             while (is.read(buffer) != -1) {// 将文件内容写到文件中  
  113.                 fos.write(buffer);  
  114.             }  
  115.             is.close();// 输入流关闭  
  116.             fos.close();// 输出流关闭  
  117.         } catch (FileNotFoundException e) {// 捕获文件不存在异常  
  118.             e.printStackTrace();  
  119.         } catch (IOException e) {// 捕获异常  
  120.             e.printStackTrace();  
  121.         }  
  122.     }  
  123.   
  124.     public static String[] listFile(File dir) {// 获取文件绝对路径  
  125.         String absolutPath = dir.getAbsolutePath();// 声获字符串赋值为路传入文件的路径  
  126.         String[] paths = dir.list();// 文件名数组  
  127.         String[] files = new String[paths.length];// 声明字符串数组,长度为传入文件的个数  
  128.         for (int i = 0; i < paths.length; i++) {// 遍历显示文件绝对路径  
  129.             files[i] = absolutPath + "/" + paths[i];  
  130.         }  
  131.         return files;  
  132.     }  
  133.   
  134.     public static void createFile(String path, boolean isFile) {// 创建文件或目录  
  135.         createFile(new File(path), isFile);// 调用方法创建新文件或目录  
  136.     }  
  137.   
  138.     public static void createFile(File file, boolean isFile) {// 创建文件  
  139.         if (!file.exists()) {// 如果文件不存在  
  140.             if (!file.getParentFile().exists()) {// 如果文件父目录不存在  
  141.                 createFile(file.getParentFile(), false);  
  142.             } else {// 存在文件父目录  
  143.                 if (isFile) {// 创建文件  
  144.                     try {  
  145.                         file.createNewFile();// 创建新文件  
  146.                     } catch (IOException e) {  
  147.                         e.printStackTrace();  
  148.                     }  
  149.                 } else {  
  150.                     file.mkdir();// 创建目录  
  151.                 }  
  152.             }  
  153.         }  
  154.     }  
  155.   
  156.     public static void main(String[] args) {// java程序主入口处  
  157.         String fromPath = "E:/createFile";// 目录路径  
  158.         String toPath = "F:/createFile";// 源路径  
  159.         System.out.println("1.移动文件:从路径 " + fromPath + " 移动到路径 " + toPath);  
  160.         try {  
  161.             fileMove(fromPath, toPath);// 调用方法实现文件的移动  
  162.         } catch (Exception e) {  
  163.             System.out.println("移动文件出现问题" + e.getMessage());  
  164.         }  
  165.         System.out.println("2.复制目录 " + toPath + " 下的文件(不包括该目录)到指定目录" + fromPath  
  166.                 + " ,会连同子目录一起复制过去。");  
  167.         copyFileFromDir(fromPath, toPath);// 调用方法实现目录复制  
  168.         System.out.println("3.复制目录 " + fromPath + "到指定目录 " + toPath  
  169.                 + " ,将目录以及目录下的文件和子目录全部复制到目标目录");  
  170.         copyDir(toPath, fromPath);// 调用方法实现目录以用目录下的文件和子目录全部复制  
  171.     }  
  172. }  
0 0