package com.ag.util;

import java.io.*;
import java.util.*;

/**
 * Created by yangxh on 2021/6/3.
 */
public class GenCategoryAttrItemHandler {
    private final static String INPUT_FILE_PATH = "E:\\npc.txt";
    private final static String OUTPUT_FLIE_PATH = "E:\\npc_site.txt";

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        List<String> source = getDataFromFile();
        List<String[]> tmp = new ArrayList<>();
        List<String> target = new ArrayList<>();
        for (String str : source){
            str = str.replace("|", ",");
            tmp.add(str.split(","));
        }
        for (String[] arr : tmp){
            arr[2] = String.format("%04d", Integer.valueOf(arr[2])).substring(0, 3)+"x";
            arr[3] = String.format("%04d", Integer.valueOf(arr[3]));
            arr[4] = arr[4].replace(" ","");
            String str = arr[2]+"_"+arr[3]+","+arr[4]+"__"+arr[0]+";"+arr[1];
            target.add(str);
        }
        Collections.sort(target);
        writeDataToFile(target);
    }

    /**
     * 从文件提取数据，格式化到程序中
     * @throws IOException
     */
    public static List<String> getDataFromFile() throws IOException {
        List<String> list = new ArrayList<>();
        BufferedReader br = null;
        try {
            File file = new File(INPUT_FILE_PATH);
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
            String line;
            while (null != (line=br.readLine())) {
                list.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != br) {
                br.close();
            }
        }
        return list;
    }

    /**
     * 将格式化的数据写入文件中
     * @throws IOException
     */
    public static void writeDataToFile(List<String> list) throws IOException {
        BufferedWriter bw = null;
        try {
            File file = new File(OUTPUT_FLIE_PATH);
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
            for (String str : list) {
                bw.write(str);  // 将数据写入文件中
                bw.newLine();        // 新建一个换行符
                bw.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                bw.close();
            }
        }
    }

}
