多文件读取

	protected final static Log log = LogFactory.getLog(TxtSort.class);
	/**
	 * 将文件排序
	 * @author liuboen
	 * @param list
	 * @param forget
	 * @param suffix
	 * @return
	 */
	public static List <String> sortFileString(List <String> list,final String forget,final String suffix){
		Collections.sort(list,new Comparator<String>(){
			@Override
			public int compare(String o1, String o2) {
				String nextString=StringUtil.substringBefore(o1,suffix);
				String currentString=StringUtil.substringBefore(o2,suffix);
				if (nextString.indexOf(forget)!=-1) {
					nextString=StringUtil.substringAfter(nextString,forget);
				}
				if (currentString.indexOf(forget)!=-1) {
					currentString=StringUtil.substringAfter(currentString,forget);
				}
				//TODO 替换中文数字到阿拉伯数字
				nextString=replaceD(nextString);
				currentString=replaceD(currentString);
				int next=Integer.parseInt(nextString);
				int current=Integer.parseInt(currentString);
				return next-current>0?1:-1;
			}
			
		});
		return list;
	}

	
	/**
	 * 用于读取多个文件
	 * @author liuboen
	 * @param args
	 */
	public static void main(String[] args) throws IOException{
		String regxString="诛仙";
		String suffix=".txt";
		String path="d:/资料/文档一期/穿越之1937/";
		File rootFile=new File(path);
		if (!rootFile.exists()||!rootFile.isDirectory()) {
			System.out.println("文件不存在");
		}
		File [] twoFiles=rootFile.listFiles();
		List <String>fileListName=new ArrayList<String>();
		for (File threeFile : twoFiles) {
			String fileName=threeFile.getName();
			if (fileName.lastIndexOf(suffix)!=-1) {
				fileListName.add(fileName);         
			}
		}
		sortFileString(fileListName,regxString,suffix);
		StringBuilder builder=new StringBuilder();
		for (int i = 0; i < fileListName.size(); i++) {
			FileReader read = new FileReader(new File(path+fileListName.get(i).toString())); 
			StringBuffer sb = new StringBuffer(); 
			char ch[] = new char[1024]; 
			int d = read.read(ch); 
			while(d!=-1){ 
			String str = new String(ch,0,d); 
			sb.append(str); 
			d = read.read(ch); 
			} 
			builder.append(sb.toString());
		}
		log.info(builder.toString().substring(0,200));
		//System.out.println(builder.toString());
	}
	/**
	 * 替换掉文件里的中英文
	 * @param replaceString
	 * @return
	 */
	public static String  replaceD(String replaceString){
		//String replaceString="aafd^()!!~``sffdsfsdf中国*&^^%$%$$$@$#{}[]###%%^^164646";
		final String regexABC="([a-zA-Z])";						//替换英文
		final String regexCN="[\u4e00-\u9fa5]*";				//替换中文
		final String regexElse="([\\p{Punct}&&[^\\$\\\\]])";	//替换特殊字符
		replaceString=replaceString.replaceAll(regexCN, "");
		replaceString=replaceString.replaceAll(regexABC,"");
		replaceString=replaceString.replaceAll(regexElse,"");
		replaceString=replaceString.replace("$", "");
		return replaceString;
	}

你可能感兴趣的:(文件读取)