JAVA 源代码 【指定文件夹 搜索指定格式的后缀名 拷贝到 指定目录】
来源:互联网 发布:网络用语及图片 编辑:程序博客网 时间:2024/06/06 07:27
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;public class CopyFile {/** * 复制单个文件 * @param oldPath String 原文件路径 如:c:/fqf.txt * @param newPath String 复制后路径 如:f:/fqf.txt * @return boolean */ public void copyFile(String oldPath, String newPath) { try { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); if (oldfile.exists()) { //文件存在时 InputStream inStream = new FileInputStream(oldPath); //读入原文件 FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; int length; while ( (byteread = inStream.read(buffer)) != -1) { bytesum += byteread; //字节数 文件大小 System.out.println(bytesum); fs.write(buffer, 0, byteread); } inStream.close(); } } catch (Exception e) { System.out.println("复制单个文件操作出错"); e.printStackTrace(); } } }
import java.io.File; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class FileViewer { /** * 读取文件夹下所以文件 * * @param dirPath * @param fileTypes * @return */ public static List<String> getAllFilePathByDir_FilterByFileType(String dirPath, List<String> fileTypes,String destination) { String localClassFilePath = FileViewer.class.getResource("/").getPath(); String contextFilePath = localClassFilePath.substring(1, localClassFilePath.length() - 16); contextFilePath = contextFilePath.replace("/", "\\"); List<String> filePathList = new ArrayList<String>(); for (int index = 0; index < fileTypes.size(); index++) { fileList = new ArrayList<String>(); List arrayList = FileViewer.getListFiles(dirPath, fileTypes.get(index), true); if (arrayList.isEmpty()) { System.out.println("没有符号要求的文件"); } else { for (Iterator i = arrayList.iterator(); i.hasNext();) { String temp = (String) i.next(); temp = temp.replace(contextFilePath, "");//把根路径去掉 System.out.println(temp); CopyFile cp=new CopyFile(); cp.copyFile(temp, destination+Util.GetReName(temp)); filePathList.add(temp); } } } for (int replaceIndex = 0; replaceIndex < filePathList.size(); replaceIndex++) { filePathList.set(replaceIndex, filePathList.get(replaceIndex).replace("\\", "/")); } return filePathList; } public static List<String> fileList = new ArrayList<String>(); /** * 添加文件后缀名称 * * @return */ public static List<String> suffixlist(String suffix) { List<String> list = new ArrayList<String>(); String[] types=suffix.split(","); for(int i=0;i<types.length;i++) { list.add(types[i]); } return list; } /** * * @param path 文件路径 * @param suffix 后缀名 * @param isdepth 是否遍历子目录 * @return */ public static List getListFiles(String path, String suffix, boolean isdepth) { File file = new File(path); return FileViewer.listFile(file, suffix, isdepth); } public static List listFile(File f, String suffix, boolean isdepth) { // 是目录,同时需要遍历子目录 if (f.isDirectory() && isdepth == true) { File[] t = f.listFiles(); for (int i = 0; i < t.length; i++) { listFile(t[i], suffix, isdepth); } } else { String filePath = f.getAbsolutePath(); if (suffix != null) { int begIndex = filePath.lastIndexOf(".");// 最后一个.(即后缀名前面的.)的索引 String tempsuffix = ""; if (begIndex != -1)// 防止是文件但却没有后缀名结束的文件 { tempsuffix = filePath.substring(begIndex + 1, filePath .length()); } if (tempsuffix.equals(suffix)) { fileList.add(filePath); } } else { // 后缀名为null则为所有文件 fileList.add(filePath); } } return fileList; }}
import java.util.List;public class Test {public static void main(String[] args) { List<String> listResult = FileViewer.getAllFilePathByDir_FilterByFileType("D:\\lib", FileViewer.suffixlist("jar"),"D:\\cordys\\"); }}
0 0
- JAVA 源代码 【指定文件夹 搜索指定格式的后缀名 拷贝到 指定目录】
- C#中拷贝指定文件夹下的所有文件夹目录到指定文件夹中的方法
- C# 拷贝文件夹到指定目录
- Java拷贝指定目录的指定类型文件到指定目录
- java 搜索指定后缀名的所有文件
- 搜索指定文件夹里指定格式的文件
- 拷贝案例——拷贝指定目录的指定类型的文件到指定目录
- 拷贝指定文件之外的所有文件到指定目录
- Java代码拷贝对应的文件到指定目录
- java文件夹复制到指定目录
- 拷贝文件到指定目录
- 拷贝文件到指定目录
- java复制文件夹下全部的文件到指定目录
- 拷贝指定目录下内容到另一个文件夹中
- Android从Assets拷贝文件以及文件夹到指定目录
- tar 压缩文件夹到指定的目录
- 获取文件夹下的文件名生成新的文件&DOC命令想从指定的txt中读出文件名,然后在指定文件夹以及子目录下搜索文件,并拷贝到指定目录
- 查找一个文件夹下指定后缀名文件的java方法
- windows任务管理器中的工作设置内存,内存专用工作集,提交大小详解
- namespace
- BZOJ 3791: 作业
- LinkedList
- Xcode bug
- JAVA 源代码 【指定文件夹 搜索指定格式的后缀名 拷贝到 指定目录】
- 10+年程序员总结的20+条经验教训
- ZOJ 3780 Paint the Grid Again
- PHP模拟登陆正方系统获取课表、成绩(一看就懂!!!)
- Flickable和Flipable
- Spring MVC 中的基于注解的 Controller
- 欢迎使用CSDN-markdown编辑器
- Linux模拟鼠标和键盘事件的方法
- LeetCode 19 - Remove Nth Node From End of List