为文件分类,小程序-解决实际中分类问题。

package com.dragon.prese;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class GenTest {
	private final static String NAME = "新增清单_有声阅读_浙江";
	private final static String POST_FIX = ".txt";
	private final static String SOURCE_PATH = "D:/lww/";
	private final static String TARGT_PATH = "D:/lww/ouput/" + NAME;
	// "PHONE_NO","CITY_NAME","CHANNEL_NAME","REG_TIME"
	// "18968985199","温州","手机客户端","2010-7-6 19:09:55"
	public static void main(String[] args) throws IOException {
		Map<String, PrintWriter> outputMap = new HashMap<String, PrintWriter>();
		Map<String, Integer> sizeMap = new HashMap<String, Integer>();

		String outfile = SOURCE_PATH + NAME + POST_FIX;// InputStream in, String
		// charsetName
		BufferedReader in = new BufferedReader(new InputStreamReader(
				new FileInputStream(outfile), "utf-8"));
		String headLine = null;
		boolean head = Boolean.TRUE;
		String line = null;
		while ((line = in.readLine()) != null) {
			if (head) {
				headLine = line;
				head = Boolean.FALSE;
			} else {
				String[] split = line.split(";");
				String name = split[1];
				if (!outputMap.containsKey(name)) {
					PrintWriter out = new PrintWriter(new BufferedWriter(
							new FileWriter(mkdirFile(TARGT_PATH + "/" + name
									+ POST_FIX))));
					outputMap.put(name, out);
					sizeMap.put(name, 1);
					out.println(headLine);
					out.flush();
				}
				PrintWriter out = outputMap.get(name);
				out.println(line);
				out.flush();
				sizeMap.put(name, sizeMap.get(name) + 1);
			}
		}
		Set<Entry<String, Integer>> set = sizeMap.entrySet();
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				mkdirFile(TARGT_PATH + "/" + "数目统计" + POST_FIX))));
		for (Entry<String, Integer> entry : set) {
			String temp = entry.getKey() + " size: " + entry.getValue();
			System.out.println(temp);
			out.println(temp);
			out.flush();
		}

	}

	public static File mkdirFile(String filepath) throws IOException {
		File file = new File(filepath);
		file.getParentFile().mkdirs();
		if (file.createNewFile()) {
			System.out.println("mkdir file " + filepath);
			return file;
		}
		System.err.println("mkdir file " + filepath);
		return null;
	}
}

你可能感兴趣的:(String,File,null,Integer,Class,Path)