Java 将特定的 csv 值存储到列表中
Java store specific csv value to list
CSV 看起来像这样:
Name;Amount;Date
Netflix;5;1.1.2021
我想为每笔费用制作不同的清单,一份用于娱乐,一份用于交通等。但是,我只想将金额存储在清单上,我该怎么做?
public class CsvReader {
public static void readDataLineByLine(String file) {
try {
// Create an object of file reader class with CSV file as a parameter.
FileReader filereader = new FileReader(file);
// create csvParser object with
// custom separator semi-colon
CSVParser parser = new CSVParserBuilder().withSeparator(';').build();
// create csvReader object with parameter
// filereader and parser
CSVReader csvReader = new CSVReaderBuilder(filereader).withCSVParser(parser).build();
// Read all data at once
List<String[]> allData = csvReader.readAll();
List<String> entertainment = new ArrayList<>();
// Print Data.
for (String[] row : allData) {
for (String cell : row) {
System.out.print(cell + "\t");
if (cell.startsWith("Netflix")){
entertainment.add(cell);
}
}
System.out.println();
System.out.println(entertainment);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
CsvReader.readDataLineByLine("tt.csv");
}
}
如果你用的是opencsv,那么写下面的代码怎么样?请审核。
public class CsvReader {
public static void readDataLineByLine(String file) {
try {
// Create an object of file reader class with CSV file as a parameter.
FileReader filereader = new FileReader(file);
// create csvParser object with
// custom separator semi-colon
CSVParser parser = new CSVParserBuilder().withSeparator(';').build();
// create csvReader object with parameter
// filereader and parser
CSVReader csvReader = new CSVReaderBuilder(filereader).withCSVParser(parser).build();
List<String> entertainment = new ArrayList<>();
// changed part
int index = 0;
while ((nextLine = reader.readNext()) != null) { // 2
// csv header exclusion condition
if(index == 0) {
continue;
}
String name = nextLine[0];
String amount = nextLine[1];
if (name.startsWith("Netflix")){
entertainment.add(amount);
}
index++;
}
// Print Data.
System.out.println();
System.out.println(entertainment);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
CsvReader.readDataLineByLine("tt.csv");
}
}
CSV 看起来像这样:
Name;Amount;Date
Netflix;5;1.1.2021
我想为每笔费用制作不同的清单,一份用于娱乐,一份用于交通等。但是,我只想将金额存储在清单上,我该怎么做?
public class CsvReader {
public static void readDataLineByLine(String file) {
try {
// Create an object of file reader class with CSV file as a parameter.
FileReader filereader = new FileReader(file);
// create csvParser object with
// custom separator semi-colon
CSVParser parser = new CSVParserBuilder().withSeparator(';').build();
// create csvReader object with parameter
// filereader and parser
CSVReader csvReader = new CSVReaderBuilder(filereader).withCSVParser(parser).build();
// Read all data at once
List<String[]> allData = csvReader.readAll();
List<String> entertainment = new ArrayList<>();
// Print Data.
for (String[] row : allData) {
for (String cell : row) {
System.out.print(cell + "\t");
if (cell.startsWith("Netflix")){
entertainment.add(cell);
}
}
System.out.println();
System.out.println(entertainment);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
CsvReader.readDataLineByLine("tt.csv");
}
}
如果你用的是opencsv,那么写下面的代码怎么样?请审核。
public class CsvReader {
public static void readDataLineByLine(String file) {
try {
// Create an object of file reader class with CSV file as a parameter.
FileReader filereader = new FileReader(file);
// create csvParser object with
// custom separator semi-colon
CSVParser parser = new CSVParserBuilder().withSeparator(';').build();
// create csvReader object with parameter
// filereader and parser
CSVReader csvReader = new CSVReaderBuilder(filereader).withCSVParser(parser).build();
List<String> entertainment = new ArrayList<>();
// changed part
int index = 0;
while ((nextLine = reader.readNext()) != null) { // 2
// csv header exclusion condition
if(index == 0) {
continue;
}
String name = nextLine[0];
String amount = nextLine[1];
if (name.startsWith("Netflix")){
entertainment.add(amount);
}
index++;
}
// Print Data.
System.out.println();
System.out.println(entertainment);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
CsvReader.readDataLineByLine("tt.csv");
}
}