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();
}
}
07-19
4354
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)