CSV 파일을 ArrayList 또는 String [] [] 배열로 읽으려고합니다. 이 목록에서 그것을 읽고 목록 배열, tokenizer 사용하여 배열에 노력하고있어. csv 파일에는 7 개의 열 (A - G)과 961 개의 행 (1-961)이 있습니다.타사 라이브러리가없는 CSV 파일 읽기
public class FoodFacts
{
private static BufferedReader textIn;
private static BufferedReader foodFacts;
static int numberOfLines = 0;
static String [][] foodArray;
public static String aFact;
static int NUM_COL = 7;
static int NUM_ROW = 961;
// Make a random number to pull a line
static Random r = new Random();
public static void main(String[] args)
{
try
{
textIn = new BufferedReader(new InputStreamReader(System.in));
foodFacts= new BufferedReader(new FileReader("foodfacts.csv"));
Scanner factFile = new Scanner(foodFacts);
List<String> facts = new ArrayList<String>();
String fact;
System.out.println("Please type in the food you wish to know about.");
String request = textIn.readLine();
while (factFile.hasNextLine()){
fact = factFile.nextLine();
StringTokenizer st2 = new StringTokenizer(fact, ",");
//facts.add(fact);
numberOfLines++;
while (st2.hasMoreElements()){
for (int j = 0; j < NUM_COL ; j++) {
for (int i = 0; i < NUM_ROW ; i++){
foodArray [j][i]= st2.nextToken(); //NULL POINTER HERE
System.out.println(foodArray[j][i]);
}
}
}
}
}
catch (IOException e)
{
System.out.println ("Error, problem reading text file!");
e.printStackTrace();
}
}
}
"CSV"라는 몇 가지 형식 중 어느 것을 읽고 싶습니까? – Raedwald
그것의 엑셀? 쉼표로 구분 된 값 –
타사 라이브러리가 없으면 이것을 수행해야합니까? –