java复制文件和java移动文件的示例分享

时间:2021-12-12 11:21:54
代码如下:

package com.famous.dark.util;

 

import java.io.File;
import java.io.FileFilter;

public class MyFileFilter implements FileFilter {

 public boolean accept(File file) {
  String fileName = file.getName().toLowerCase();
  if (fileName.endsWith(".jar") || file.isDirectory()) {
   return true;
  }
  return false;
 }
}

 

 

复制代码代码如下:

package com.famous.dark.util;

 

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

/**
 * 
 * @author pat 将文件中的jar文件提取出来
 */
public class JarCollectionUtil {

 public String collectionJar(File baseFile, String targetFilePath) throws Exception {
  File targetDir = new File(targetFilePath);
  if (!targetDir.exists()) {
   targetDir.mkdir();
  }

  if (baseFile.exists() && baseFile.isDirectory()) {
   File[] files = baseFile.listFiles(new MyFileFilter());
   for (File file : files) {
    if (file.isDirectory()) {
     collectionJar(file, targetFilePath);
    } else {
     File targetFile = new File(targetFilePath + "\\" + file.getName());
     FileUtils.copyFile(file, targetFile);

    }
   }
   return "success";
  } else {
   return "文件路径不存在";
  }

 }

 public static void main(String[] args) throws Exception {
  JarCollectionUtil collectionUtil = new JarCollectionUtil();
  collectionUtil.collectionJar(new File("F:\\JAR\\lucene-4.3.1"), "F:\\JAR\\LUCENE");
 }
}