解析OUT文件
2018-07-04 本文已影响0人
Bre_eze
public void parseOUT() throws IOException {
long startTime = System.currentTimeMillis();
final String filename = "D:\\TEMCONH.OUT";
File file = new File(filename);
List<String> list = new ArrayList<String>();
if (file.exists() && file.isFile()) {
InputStreamReader read = new InputStreamReader(new FileInputStream(file));
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
int count = 0;
while ((lineTxt = bufferedReader.readLine()) != null) {
count++;
if (count > 4) {
list.add(lineTxt);
}
}
bufferedReader.close();
read.close();
}
int[] cols = new int[list.size()];
int[] rows = new int[list.size()];
double[] salis = new double[list.size()];
for (int i = 0; i < list.size(); ++i) {
String[] strs = list.get(i).split("\\s+");
if (strs.length >= 4) {
cols[i] = Integer.parseInt(strs[1]);
rows[i] = Integer.parseInt(strs[2]);
salis[i] = Double.parseDouble(strs[3]);
}
}
long endTime = System.currentTimeMillis();
for (int i = 0; i < salis.length; i++) {
System.out.println(cols[i]);
System.out.println(rows[i]);
System.out.println(salis[i]);
System.out.println();
}
System.out.println("共 " + salis.length + " 条, 耗时 " + (endTime - startTime) + " ms");
}
更新算法:
public void parseOUT() throws IOException {
long startTime = System.currentTimeMillis();
final String filename = "D:\\TEMCONH.OUT";
final Integer COUNT = 463*36;
File file = new File(filename);
int[] cols = new int[COUNT];
int[] rows = new int[COUNT];
double[] salis = new double[COUNT];
if (file.exists() && file.isFile()) {
InputStreamReader read = new InputStreamReader(new FileInputStream(file));
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
int allCount = 0;
int itemCount = 0;
while ((lineTxt = bufferedReader.readLine()) != null) {
allCount++;
if (allCount > 4) {
String [] strs = lineTxt.split("\\s+");
if (strs.length >= 4) {
cols[itemCount] = Integer.parseInt(strs[1]);
rows[itemCount] = Integer.parseInt(strs[2]);
salis[itemCount] = Double.parseDouble(strs[3]);
itemCount++;
}
}
}
bufferedReader.close();
read.close();
}
long endTime = System.currentTimeMillis();
// for (int i = 0; i < salis.length; i++) {
// System.out.println(cols[i]);
// System.out.println(rows[i]);
// System.out.println(salis[i]);
// System.out.println();
// }
System.out.println("共 " + salis.length + " 条, 耗时 " + (endTime - startTime) + " ms");
}