我有一个CSV文件,其中包含Customer Order Details
,格式为:
OrderID Name Status
我想将Order ID
和Status
存储在String[][]
中。
CSVReader reader = new CSVReader(new FileReader(FILE_PATH));
String nextLine;
while ((nextLine = reader.readNext()) != null)
{
System.out.println(nextLine[0]);
}
答案 0 :(得分:0)
尝试一下:
import com.opencsv.CSVReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Paths;
public class OpenCSVReader {
private static final String SAMPLE_CSV_FILE_PATH = "./users.csv";
public static void main(String[] args) throws IOException {
try (
Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
CSVReader csvReader = new CSVReader(reader);
) {
// Reading Records One by One in a String array
String[] nextRecord;
while ((nextRecord = csvReader.readNext()) != null) {
System.out.println("Name : " + nextRecord[0]);
System.out.println("Email : " + nextRecord[1]);
System.out.println("Phone : " + nextRecord[2]);
System.out.println("Country : " + nextRecord[3]);
System.out.println("==========================");
}
}
}
}
答案 1 :(得分:0)
使用列表的示例:
import java.io.FileReader;
import java.util.Arrays;
import java.util.List;
import au.com.bytecode.opencsv.CSVReader;
public class ParseFullCSVExample
{
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception
{
//Build reader instance
CSVReader reader = new CSVReader(new FileReader("data.csv"), ',', '"', 1);
//Read all rows at once
List<String[]> allRows = reader.readAll();
for(String[] row : allRows){
System.out.println(Arrays.toString(row));
}
}
}