欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

分享一段归集同类型文件的代码 Java文件操作

程序员文章站 2022-07-14 21:17:53
...

        解决的问题:一个超级庞大的文件加,里面有一些我们需要的文件, 把他们找出来。 具体自己看吧,用的上就拿去。

 

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.

 

相关标签: Java 文件操作