我可以使用下面的代码将CSV文件读取到我的Javamain方法中。
我想要实现的是能够在而不是mymain方法中使用以下代码。我希望能够从main调用此方法,这样我就可以读取CSV文件,而不会让所有这些代码扰乱我的main。我该怎么做?
仅供参考,CSV文件有两列双精度,因此使用了double [][]
。
public static void main(String[] args) throws IOException {
double [][] data = new double [100][2];
File file = new File("C:\Users\Username\Java\Test2\First\src\Program1\prac.csv");
int row = 0;
int col = 0;
BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String line = null;
//read each line of text file
while((line = bufRdr.readLine()) != null && row < data.length) {
StringTokenizer st = new StringTokenizer(line,",");
while (st.hasMoreTokens()) {
//get next token and store it in the array
data[row][col] = Double.parseDouble(st.nextToken());
col++;
}
col = 0;
row++;
}
}
定义一个名为CSVReader-的自定义类
public class CSVReader {
public List<List<Double>> read (File file) throws IOException {
List<List<Double>> data = new ArrayList<List<Double>>();
BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String line = null;
//read each line of text file
while((line = bufRdr.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line,",");
List<Double> row = new ArrayList<Double>();
while (st.hasMoreTokens()) {
//get next token and store it in the array
row.add(Double.parseDouble(st.nextToken()));
}
data.add(row);
}
return(data);
}
}
然后,在主中
public static void main(String[] args) {
List<List<Double>> data;
String fileName = "C:\Users\Username\Java\Test2\First\src\Program1\prac.csv";
File file = new File(fileName);
try {
CSVReader reader = new CSVReader();
data = reader.read(file);
}
catch (IOException ex) {
// handle your exception
}
}
请注意,我使用了List
s而不是数组来读取数据。这比使用二维数组要干净得多-我不再需要执行任何绑定检查,也不需要增加数组索引等。你可以阅读Effective Java,Item 25-preferred lists to arrays-了解更多关于优点的信息。
只需将其移动到一个新方法中:
public static double[][] readData(String fName){
double [][] data = new double [100][2];
File file = new File(fname);
int row = 0;
int col = 0;
BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String line = null;
//read each line of text file
while((line = bufRdr.readLine()) != null && row < data.length) {
StringTokenizer st = new StringTokenizer(line,",");
while (st.hasMoreTokens()) {
//get next token and store it in the array
data[row][col] = Double.parseDouble(st.nextToken());
col++;
}
col = 0;
row++;
}
return(data);
}
由于是静态的,可以从main调用它,而无需实例化类。
我相信这对您的代码来说非常容易。只需创建另一个名为readCSV的方法并返回数据,就可以在主方法中调用readCSV。readCSV中的代码正是您现在在主方法中所做的。
顺便说一句,我注意到,在阅读器完成工作后,你不会关闭它,你试图在main方法中返回一个值,而main的返回类型应该是void,即nothing。
试试这个:
public static void main(String[] args) throws IOException {
double[][] data = readFile("C:\Users\Username\Java\Test2\First\src\Program1\prac.csv");
}
public static double[][] readFile(String filepath) throws NumberFormatException, IOException {
double[][] data = new double[100][2];
File file = new File(
filepath);
int row = 0;
int col = 0;
BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String line = null;
// read each line of text file
while ((line = bufRdr.readLine()) != null && row < data.length) {
StringTokenizer st = new StringTokenizer(line, ",");
while (st.hasMoreTokens()) {
// get next token and store it in the array
data[row][col] = Double.parseDouble(st.nextToken());
col++;
}
col = 0;
row++;
}
return (data);
}