0
我想將CSV數據文件中的數據分配到二維數組中。我寫了下面的代碼,但它只適用於一維數組。只要我在下面的行中添加多餘的方括號,就會發生錯誤。我對Java真的很陌生。請幫忙。從CSV數據填充二維數組
String[] pTEDATA = line.split(cvsSplitBy); // Before
String[][] pTEDATA = line.split(cvsSplitBy); // After
這裏是整個代碼:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class pTE {
public static void main(String[] args) {
pTE obj = new pTE();
obj.run();
}
public void run() {
String csvFile = "C:/Users/marathnayake/Downloads/new.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
if(line.equals("")){
continue;
}
String[][] pTEDATA = line.split(cvsSplitBy);
//while (pTEDATA != null){
//System.out.println("[\'" + pTEDATA[0] + "\'], " + "[\'" + pTEDATA[1] + "\'], " + "[\'" + pTEDATA[2] + "\']");
//}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}