我可以使用下面的代码将CSV文件读入我的Java main 方法。
我想要实现的是能够在不 main 方法的方法中使用以下代码。我希望能够从我的主中调用此方法,以便我可以读取CSV文件,而不会让所有这些代码混乱我的主要。我该怎么做?
仅供参考,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++;
}
}
答案 0 :(得分:1)
定义名为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
而不是数组来读取数据。这比使用二维数组更清晰 - 我不再需要执行任何绑定检查,也不需要增加数组索引等。您可以阅读Effective Java,第25项 - 首选列表到数组 - 了解有关好处的更多信息。
答案 1 :(得分:0)
只需将其移至新方法:
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调用而不实例化类。
答案 2 :(得分:0)
我相信你的代码很容易。您只需创建另一个名为readCSV的方法并返回数据,然后您可以在main方法中调用readCSV。 readCSV中的代码正是您现在在main方法中所做的。
顺便说一句,我注意到你完成它的工作后你没有关闭你的读者,并且你试图在main方法中返回一个值,而main的返回类型应该是无效的,即没有。答案 3 :(得分:0)
试试这个:
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);
}