-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_bar_parse_struct.java
95 lines (95 loc) · 2.61 KB
/
csv_bar_parse_struct.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.Date;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;
import java.nio.file.attribute.BasicFileAttributes;
import java.io.IOException;
class Bar
{
public String dTime;
public String day;
public float open;
public float close;
public float high;
public float low;
public long volume;
}
public class csv_bar_parse_struct
{
public static long currms()
{
return System.currentTimeMillis();
}
public static long elap(String msg, long start)
{
long elap = currms() - start;
System.out.println("elap " + msg + " elap= " + elap);
return elap;
}
public static float averageS(Bar[] bars)
{
double total = 0.0;
for (Bar aBar : bars)
{
total += aBar.close;
}
return (float) (total / bars.length);
}
public static void TestWithStruct(String fiName)
{
try
{
System.out.println("TEST WITH STRUCT ARR");
Path p = Paths.get(fiName);
System.out.println("Size : " + Files.size(p));
long start = currms();
long startRun = start;
List<String> lines = Files.readAllLines(p, Charset.forName("US-ASCII"));
elap("read File lines", start);
int numRow = lines.size() - 1;
start = currms();
Bar[] bars = new Bar[numRow];
elap("Create Empty Bar Arr", start);
start = currms();
Iterator iterator = lines.iterator();
String headLine = (String) iterator.next();
int ndx = 0;
while(iterator.hasNext())
{
String aRow = (String) iterator.next();
if (aRow.length() < 5)
continue;
Bar aBar = new Bar();
String[] strArr = aRow.split(",");
aBar.dTime = strArr[0];
aBar.day = strArr[1];
aBar.open = Float.parseFloat(strArr[2]);
aBar.close = Float.parseFloat(strArr[3]);
aBar.high = Float.parseFloat(strArr[4]);
aBar.low = Float.parseFloat(strArr[5]);
aBar.volume = Long.parseLong(strArr[6]);
bars[ndx] = aBar;
ndx++;
}
elap("Finished ieterator", start);
elap("Finished complete load ", startRun);
start = currms();
float tavg = averageS(bars);
elap("Calc Average ", start);
System.out.println("average close = " + tavg);
}
catch (IOException e)
{
System.err.println("Error: " + e.toString());
}
} // end Test with Vectors
public static void main(String[] args)
{
TestWithStruct("2014.M1.csv");
}
}