0
나는 csv 파일을 읽는 자바 코드가 있지만 출력은 NULL이다. 코드는 파일을 읽고 double 배열에 할당합니다.File Reader Return Null
import java.io.*;
import java.util.*;
public class CSVReader {
public static void main(String[] args) throws IOException {
try {
double[][] Data = null;
Data = CSVReader.read("C:\\Users\\mrezz\\Data.csv", true);
System.out.println(Arrays.deepToString(Data));
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static double[][] read(String theFile, boolean hasTitle) throws IOException {
try {
boolean firstTime = true;
double[][] data = null;
File file = new File(theFile);
int row = 0;
int col = 0;
BufferedReader reader = new BufferedReader(new FileReader(file));
bufferedReader.close();
String line;
if (hasTitle) {
line = reader.readLine();
}
while ((line = reader.readLine()) != null) {
if (firstTime) {
firstTime = false;
StringTokenizer st = new StringTokenizer(line, ",");
col = 0;
while (st.hasMoreTokens()) {
st.nextToken();
col++;
}
data = new double[row][col];
col = 0;
continue;
}
StringTokenizer st = new StringTokenizer(line, ",");
while (st.hasMoreTokens()) {
data[row][col] = Double.valueOf(st.nextToken());
col++;
}
col = 0;
row++;
}
return data;
} catch (Exception e) {
return null;
}
}
}
코드는 파일을 (double [] [] 데이터) 배열에 할당해야합니다.
도움 바랍니다
예외는 작업이 실패했음을 나타내며 작업이 성공한 것처럼 계속하는 것은 적절하지 않습니다. 예외를 무시하지 마십시오! 'read' 메소드에서 try/catch를 제거하십시오. 'main' 메소드에서 System.out.println (e.getMessage()) 행을'e.printStackTrace();'로 대체하십시오. 예외는 정확히 무엇이 잘못되었는지, 어디서 잘못되었는지 알려줍니다. – VGR
@ VGR 나는 Arrayindexoutofbound Excption을 가지고 있습니까 ?? – novin
예외의 스택 추적을보십시오. 예외가 발생한 코드 줄을 보여줍니다. 질문을 편집하고 그 정보를 우리와 공유하면 답을 얻을 확률이 높아집니다. – VGR