今天写的拷贝某个目录下所有符合要求的文件名到某个指定文件名的工具类

今天因为想把eclipse语言插件下的所有jar包的名字放到build.xml中,一开始用F2的方法手工的拷名字,结果458个文件实在太烦了,就写了个工具类,大家可以参考。

package org.sunway.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class ReadFileName {

	private static String prefx = "";
	private static String suffix = "";
	public static String getPrefx() {
		return prefx;
	}
	public static void setPrefx(String prefx) {
		ReadFileName.prefx = prefx;
	}
	public static String getSuffix() {
		return suffix;
	}
	public static void setSuffix(String suffix) {
		ReadFileName.suffix = suffix;
	}
	
	public String readFileNamesFromDirectory(String absolueFilePath,final String fileSuffix){
		StringBuffer sb = new StringBuffer();
		File dir = new File(absolueFilePath);
		if(dir.exists() && dir.isDirectory()){
			File[] files = dir.listFiles(new FilenameFilter(){

				public boolean accept(File dir, String name) {
					// TODO Auto-generated method stub
					return name.endsWith(fileSuffix);
				}
				
			});
			for(int i=0;i<files.length;i++){//没有考虑目录下还有目录的情况
				if(files[i].isFile()){
					sb.append("			")
					.append(getPrefx())
					.append(files[i].getName())
					.append(getSuffix())
					.append("\n");
				}
			}
		}
		return sb.toString();
	}
	/**
	 * 生成文件
	 * */
	public static void write(String  filename, String s) throws IOException {
		write(new File(filename), s, "UTF-8");
	}
	
	public static void write(File file, String s) throws IOException {
		write(file, s, "UTF-8");
	}

	public static void write(File file, String s, String encoding)
			throws IOException {
		if (file.getParent() != null)
			mkdirs(file.getParent());
		OutputStreamWriter bw = new OutputStreamWriter(new FileOutputStream(
				file), encoding);
		bw.flush();
		bw.write(s);
		bw.close();
	}
	
	/**
	 * 创建目录
	 * */
	public static void mkdirs(String pathName) {
		File file = new File(pathName);
		if (!file.exists())
			file.mkdirs();
	}

	
	  //取得指定扩展名的文件列表
	  public static FilenameFilter getFileExtensionFilter(String extension) {
		final String _extension = extension;
		return new FilenameFilter(){
			public boolean accept(File dir, String name) {
				// TODO Auto-generated method stub
				return (name.endsWith(_extension) && dir.isFile());
			}
			
		};
	  }
	  
	  public static void main(String[] args) {
		ReadFileName readFileName = new ReadFileName();
		String prefix = "<pathelement path=\"${languagePlugin}/plugins/";
		readFileName.setPrefx(prefix);
		readFileName.setSuffix("\"/>");
		try {
			String content = readFileName.readFileNamesFromDirectory("E:\\Language\\Language\\plugins", "jar");
			readFileName.write("D:\\temp.txt", content);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

你可能感兴趣的:(eclipse,xml,D语言)