Orangee实验数据处理

package DataProcess;
import java.io.*;
import java.util.ArrayList;

public class DataProcess2 {
    static String sourcePath = "D:\\orange\\source";
    static String outPath = "D:\\orange\\out";
    static String[][] types = new String[3][];


    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        File folder = new File(sourcePath);
        File outputFile = new File(outPath +"\\out.csv");
        File[] files = folder.listFiles();
        String[] fileLabels = decodeNameToLabel(files);
        int[][][] tables = process(files);
        try {
            outputToCSV(tables, fileLabels, outputFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println((end - start) + "ms");
    }


    public static int[][][] process(File[] files){
        int[][][] res = new int[files.length][][];
        for (int i = 0; i < files.length; i++) {
            ArrayList<int[]> list = new ArrayList<>();
            BufferedReader reader = getReader(files[i]);
            String row = null;
            int[] val;
            boolean start = false;
            int lastTime = -1;
            while(true){

                try {
                    if ((row = reader.readLine()) == null) break;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                val = new int[3];
                int valIndex = 0;
                char c;
                for(int j = 0; j < row.length(); j++){
                    if(' ' == (c = row.charAt(j))){
                        valIndex++;
                    }else{
                        val[valIndex] *= 10;
                        val[valIndex] += c - '0';
                    }
                }
                if(!start && !(start = val[1] == 0)) continue;
                if(lastTime == (lastTime = val[0])) continue;
                list.add(val);
            }
            res[i] = new int[list.size()][];
            list.toArray(res[i]);
        }
        return res;
    }


    public static String[] decodeNameToLabel(File[] files){
        int num = files.length;
        String[] labels = new String[num];
        types[0] = new String[]{"merge", "no"};
        types[1] = new String[]{"3000", "4000", "5000"};
        types[2] = new String[]{"cf2", "cf3", "cf4"};
        for (int i = 0; i < num; i++) {
            String fileName = files[i].getName();
            StringBuilder fileLabel = new StringBuilder();
            for(String[] type : types){
                for (String label : type) {
                    if (fileName.lastIndexOf(label) != -1) {
                        fileLabel.append(label + '-');
                        break;
                    }
                }
            }
            fileLabel.deleteCharAt(fileLabel.length() - 1);
            labels[i] = fileLabel.toString();
        }
        for(String label : labels) System.out.println(label);
        return labels;
    }


    public static BufferedReader getReader(File file){
        BufferedReader br = null;
        try {
             br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return br;
    }


    public static BufferedWriter getWriter(File file){
        if(!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        BufferedWriter bw = null;
        try {
            FileWriter fw = new FileWriter(file);
            bw = new BufferedWriter(fw);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bw;
    }


    public static void outputToCSV(int[][][] tables, String[] fileLabels, File outputFile) throws IOException {
        BufferedWriter writer = getWriter(outputFile);
        int fileNum = fileLabels.length;
        StringBuilder row = new StringBuilder();
        int maxlength = 0;
        for (int i = 0; i < fileNum; i++) {
            row.append(fileLabels[i] + ",,,");
            maxlength = Math.max(maxlength, tables[i].length);
        }
        row.deleteCharAt(row.length() - 1);
        writer.write(row.toString());
        writer.newLine();
        for (int i = 0; i < maxlength; i++) {
            row = new StringBuilder();
            for (int j = 0; j < fileNum; j++) {
                if(i >= tables[j].length){
                    row.append(",,,");
                }
                else{
                    for (int k = 0; k < 3; k++) {
                        row.append(tables[j][i][k] + ",");
                    }
                }
            }
            row.deleteCharAt(row.length() - 1);
            writer.write(row.toString());
            writer.newLine();
        }
        writer.flush();
        writer.close();
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值