解决的问题:一个超级庞大的文件加,里面有一些我们需要的文件, 把他们找出来。 具体自己看吧,用的上就拿去。
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.nio.channels.FileChannel; import org.apache.commons.io.FileUtils; /** * @author Stony Zhang (zhangyu0182@sina.com) * http://mybeautiful.iteye.com */ public class BatchFileRename { private File targetFolder; private String soureFolder; private String extName; // such as ".png" private FileNameHander nameHandler; private FileWriter contentFileWriter; public BatchFileRename(String soureFolder, String targetFolder, String extName) { this(soureFolder,targetFolder,extName, true); } public BatchFileRename(String soureFolder, String targetFolder, String extName, boolean cleanTarget) { this.soureFolder = soureFolder; this.extName = extName; this.targetFolder = new File(targetFolder); if(!this.targetFolder.exists()){ this.targetFolder.mkdirs(); } if (cleanTarget){ try { FileUtils.cleanDirectory(this.targetFolder); } catch (IOException e) { e.printStackTrace(); } } try { contentFileWriter = new FileWriter(new File(targetFolder, "content.txt"), true); } catch (IOException e) { e.printStackTrace(); } } public void execute() { File sourceDir = new File(this.soureFolder); if (!sourceDir.isDirectory()) { System.out.println("Pls specified an correct forder!"); return; } scanFolder(sourceDir); try { contentFileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } private void scanFolder(File sourceDir) { File[] list = sourceDir.listFiles(); for (File file : list) { if (file.isDirectory()) { scanFolder(file); continue; } if (!file.getName().endsWith(extName)) { continue; } this.copyToTargetFolder(file); } } private void copyToTargetFolder(File file) { try { // check if the file exists? String fileName = this.getFileName(file); System.out.println("Copy file :" + file.getAbsolutePath()); contentFileWriter.write(String.format("%-40s %s\r\n", fileName, file.getAbsolutePath())); contentFileWriter.write(String.format("%80s\r\n", "_").replace(" ", "_")); copyFile(file, new File(this.targetFolder, fileName)); } catch (IOException e) { e.printStackTrace(); } } private String getFileName(File file) { String fName = getNameHandler().newName(file); File destFile = new File(this.targetFolder, fName); if (!destFile.exists()) { return fName; } String newFileName; int index = fName.lastIndexOf("."); if (index > 0) { String mainName = fName.substring(0, index); String extName = fName.substring(index); newFileName = mainName + "_1" + extName; } else { newFileName = fName + "_1"; } File newFile = new File(this.targetFolder, newFileName); if (newFile.exists()) { return getFileName(newFile); } return newFileName; } public static void copyFile(File in, File out) throws IOException { FileInputStream fileInputStream = new FileInputStream(in); FileOutputStream fileOutputStream = new FileOutputStream(out); try { FileChannel sourceChannel = fileInputStream.getChannel(); FileChannel destinationChannel = fileOutputStream.getChannel(); sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel); // or destinationChannel.transferFrom(sourceChannel, 0, // sourceChannel.size()); fileInputStream.close(); fileOutputStream.close(); sourceChannel.close(); destinationChannel.close(); } catch (Exception e) { e.printStackTrace(); } finally { } } public static void main(String[] args) { BatchFileRename run = new BatchFileRename("f:\\temp\\test_src", "F:\\temp\\test_dest", ".png"); run.setNameHandler(new FileNameHander() { @Override public String newName(File file) { return file.getName().toLowerCase(); } }); run.execute(); } public FileNameHander getNameHandler() { return nameHandler; } public void setNameHandler(FileNameHander nameHandler) { this.nameHandler = nameHandler; } private static interface FileNameHander { String newName(File file); } }
注意: 依赖common-io.
相关推荐
多EXCEL文件的数据自动归集与汇总多EXCEL文件的数据自动归集与汇总多EXCEL文件的数据自动归集与汇总
国民经济非垂直管理行业或领域建立大数据中心,需要配备能大规模云同步归集行业数据的软件系统,“行业数据云通用的同步枢纽与大数据联合体平台”(GSMS)就是为此而研制的。GSMS主要用于通过互联网大规模同步采集...
市场监管大数据归集系统建设研究.pdf
费用归集工具(会计通用版)1.25 版权:渊龙科技 BY:天蝎的怨恨 F2: 添加框与查询框的切换键 回车:光标移动到下一个输入框 及“确定” 重置费用项目:设置 费用项目的名称 如:运费 此软件为 会计核算费用成本的...
1、仅需一秒,自动监听指定钱包地址余额,一有余额进账(链上归集仅需1秒),自动转出归集到指定钱包。 2、多签机制,一个钱包,多个私钥分权限管理,更加安全。 3、采用go语言开发,百万协程goroutine机制并发能力...
数据归集的内容及数据标准要求.pdf
电梯应急处置平台数据归集规则.doc
省公共信用信息系统第一批归集数据项.doc
成本会计习题(2)练习生产费用的归集和分配答案.doc
1、支持以太坊、BSC、TRON多账户余额监控通知、自动归集 2、可自定义Telegram机器人账号通知 3、可自定义多账户支持、设置余额通知以及归集数值、阈值,灵活配置、一键支持方便灵活
商业银行如何开展住房公积金归集及贷款业务.pptx
电力行业量测数据归集专项模型及标准表设计成果
对一中心、一张图、服务接口和运行指标体系进行完善优化。基本搭建水务数据大基座,大屏版、电脑版和移动版。 为全市“一网统管”建设、水务数字化转型工作提供了数据支撑。 在防御2018-2020年“安比”、“云雀”、...
(信用管理)省公共信用信息系统第一批归集数据项.pdf
金蝶KIS产品课件_ZYB10.0_生产管理(成本归集).ppt
深信服_下一代防火墙AF_央企互联网出口归集安全防护场景.pdf
技术资料整理归集系列之vSphere虚拟化优化与排错.pdf
完整Java、Springboot对接钱包,ETH、BSC、HECO钱包充提、归集
Python代码笔记 代码笔记
“一网统管”需求下的水务数据归集与服务研究.pdf