从这里开始,这是一个延续:
我现在已经创建了两个类。
1)历史资料
2)期货。
下面是代码:它执行以下操作:接收一个文件,检查它是否是一个文件,根据文件的大小动态创建一个数组,然后将其打印为一个字符串。正如你所看到的,这仍然是程序性的。它可以工作,但我不使用OOP主体。
import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.StringTokenizer;
/*
* This will allow me to uploadFutures historical csv data into an arraylist
* I then want to perform analysis on it
*/
public class Futures extends HistoricalData{
// absolute path of my file
File esFuturesData = new File("C:/Users/Administrator/Desktop/Stock Project/Tick Data Source Files/TextFiles/Completed/test.txt");
// need to declare an list to loop through it
public ArrayList<E> myList = new ArrayList<E>();
// need a method to see if the file can be read.
public boolean isaFile()
{
return esFuturesData.isFile();
}
// need a method to get the size of the file ..
public int sizeofFile() throws IOException{
// variable z for size of file
int z = 0;
if(isaFile()){
// read in the file esFuturesData and store it in object reader
BufferedReader reader = null;
try{
reader = new BufferedReader(new FileReader(esFuturesData));
// need to find out how many rows are in the file
while(reader.readLine() != null)
{
//when there is new line increment z by 1
z +=1;
}
} catch (IOException ex) {
System.err.println();
}
finally{
if(reader != null){
reader.close();
}
}
}
else{
System.out.println("This is not a file");
}
return z;
}
// need to create a dynamic String array based on the size of the file
public void createArray(){
// reset the rows of the file to 0.
int rowCount =0;
// create a testData array based on the number of rows method and that is of 4 columns
myList = new ArrayList[sizeofFile()][5];
// we need to read in the file..
// catch any errors..
BufferedReader reader = null;
try{
reader = new BufferedReader(new FileReader(esFuturesData));
// we need to create a line of type String to store what we are putting through it..
String line = null;
// need to keep reading until there is nothing left.
while((line=reader.readLine()) != null){
// we need to put the line into the array testData
StringTokenizer p = new StringTokenizer(line,",");
// while there is something to read
while(p.hasMoreTokens()){
// loop through the first column and add a 1
for (int columnCount = 0; columnCount<1;columnCount++){
// need to convert to string representation of an int
myList[rowCount][columnCount] = "0";
}
// loop through the columns
for (int columnCount = 1; columnCount<5;columnCount++){
myList [rowCount][columnCount] = p.nextToken();
//System.out.printf("%s", testData [x][y]);
} // end of for loop
rowCount++;
} // end of while loop
} // end of other while loop
} // end of try braces
catch(Exception e){
System.err.println();
} // end of catch
} // end of create array braces
// method that prints the file as a String
public void printArrayAsString(){
// Some Housekeeping
System.out.printf(" ID");
System.out.printf(" Date");
System.out.printf(" Time ");
System.out.printf(" Price");
System.out.printf(" Volume");
System.out.println();
// need a loop to go through each row number
for(int rowCount = 0; rowCount < myList.length; rowCount++ ){
// print out each row number
// System.out.printf("%s", x);
// print out a space
System.out.printf(" ");
{
for(int columnCount = 0; columnCount < 5; columnCount++ ){
System.out.printf("%s", myList [rowCount][columnCount]);
System.out.printf(" ");
}
System.out.println();
}
}
}
}发布于 2013-05-27 07:53:20
首先,您的缩进、大括号样式和空格的使用都是混合/混乱的,如果可用,请在您最喜欢的IDE中点击“格式代码”(点击"Format Code“)。
// reads in a esfutures file and stores it as esfuturesdataobject Java文档通常如下所示:
/**
* This is an important variable
*/
private String awesomeness = null;这样就可以让IDE和其他系统接收到它们和f.e。直接展示给你。
File esfuturesdata = new File("C://Users//Administrator//Desktop//Stock Project//Tick Data Source Files//TextFiles//Completed//test.txt");\,不需要转义的斜杠/,所以它是一个或另一个: C:\Users\Administrator\.C:/用户/管理员/.private,public)以提高清晰度。// need to declare an tesdata Array for looping through my data
String [][] testData;考虑一下在这种情况下List<T>是否更有趣。
BufferedReader reader = new BufferedReader(new FileReader(futuresdata));读者和类似的应该在使用后关闭。关于这样做的可能设计如下:
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
// Usage goes here.
} catch (IOException ex) {
// Exception handling goes here.
} finally {
if(reader != null) {
reader.close();
}
}catch (Exception e)不要捕获一般的异常,只捕获必要的异常并优雅地处理它们。还要考虑是否需要抛出这些文件,因为如果由于任何原因无法读取文件,应用程序将很高兴地继续运行。
System.out.println("e");那是什么?!更不用说错误应该是stderr,也就是System.err.println()。
// reset the rows of the file to 0.
int x =0;如果该变量包含行数,那么也请给它一个有意义的名称,如rows行rowCount。
}// end of futures class brackets如果您的代码结构良好且意图正确,则此类注释是不必要的。
发布于 2013-05-27 13:06:00
我不知道我是否理解当前的课程设计。HistoricalData是什么样子的?
想想你正在做的不同的事情,它们代表了什么,以及你想用它们做什么。然后将它们建模为类。
例如,我看到一些表示CSV文件中一行的内容。我可以看到它有ID,日期,价格等东西。目前,这些似乎只是数组中的字符串。考虑使用适当数据类型的属性(例如,日期和时间的java.util.Date )创建一个类来表示这一点。也许类似于HistoricalDataElement(?)也许你能想出一个更好的名字。您会将CSV文件中一行中的数据称为什么?
创建一个单独的类来表示HistoricalDataElements的集合也可能是有意义的。这可能是一个很好的地方,可以将一些操作在整个数据集上的函数放在这里。您必须决定这对您的场景是否有意义。也许你会有多个类来做不同类型的分析。
您还应该考虑将解析分离到它自己的类中。
以下是我正在思考的一个非常粗略的概要:
public class HistoricalDataElement {
private String id;
private Date dateTime;
private BigDecimal price;
private long volume;
// getters/setters
}
public class HistoricalData {
private List<HistoricalDataElement> historicalDataElements;
public long doSomeAnalysis() {...}
public long doSomeOtherAnalysis() {...}
// etc
}
public class HistoricalDataParser {
public HistoricalData parse(File file) {...}
}https://codereview.stackexchange.com/questions/26637
复制相似问题