`

一个变态的统计文件中log不重复字符的个数的程序

    博客分类:
  • work
 
阅读更多

package com.xiaxing.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileOperator {
	private static final String FILTER = "xxxx - [lineId : ";
	private static List<String> mValidNumList = new ArrayList<String>(); 

	public static class Logger {
		public Logger() {
		}

		public static void v(String string) {
			System.out.println(string);
			return;
		}
	}

	private static int readFileByLine(String filePath) {
		File file = new File(filePath);
		BufferedReader reader = null;
		Integer totallinecount = 0;
		Integer validlinecount = 0;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = "";
			String curString = "";
			String oldString = "";
			while ((tempString = reader.readLine()) != null) {
				// Logger.v(curString);
				if (!tempString.contains(FILTER))
					continue;

				curString = getValidString(tempString);

				if (curString == null) {
					Logger.v("********************* error **********************");
				}

				totallinecount++;

				if (oldString.equalsIgnoreCase(curString)) {
					oldString = curString;
					continue;
				}

				oldString = curString;
				
				//whether lineid already have
				if (mValidNumList.contains(curString)) {
					Logger.v("string already have in the list");
					continue;
				}
				
				mValidNumList.add(curString);
				validlinecount++;
			}
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			Logger.v("total line number : " + totallinecount.toString() + "\n"
					+ "valid line number : " + validlinecount + "\n"
					+ "------------------------- File Read Over " + "\n\n");
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return validlinecount;
	}

	private static void readDir(String filePath) {
		File dir = new File(filePath);
		File[] files = dir.listFiles();
		int validcount = 0;
		for (int i = 0; i < files.length; i++) {
			String tempFile = files[i].toString();
			Logger.v("------------------------- File Read Start, FileName : "
					+ tempFile);
			int count = readFileByLine(tempFile);
			validcount = validcount + count;
		}
		Logger.v("======================================" + "\n"
				+ "Total valid count : " + validcount + "\n"
				+ "======================================");
	}

	private static String getValidString(String origString) {
		String output = null;
		String temp = origString;
		if (temp.contains(FILTER)) {
			int index = temp.indexOf(FILTER);
			int count = FILTER.length();
			output = temp.substring(index + count, index + count + 5);
			// Logger.v("true");
		} else {
			// Logger.v("false");
		}
		return output;
	}

	public static void main(String[] args) {
		String string = "E:\\temp";
		// readFileByLine(string);
		readDir(string);

		// String temp = "fuck fucka --- xxxx - [lineId : 30222]";
		// String output = getValidString(temp);
		// Logger.v(output);
	}
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics