在 Java 中解析 CSV 文件时出现问题
Problems while parsing a CSV file in Java
在此代码中,目的是解析 CSV 文件并将其数据映射到 bean object。
ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
strat.setType(Country.class);
String[] columns = new String[] {"countryName", "capital"};
strat.setColumnMapping(columns);
CsvToBean csv = new CsvToBean();
String csvFilename = "C:/Users/user/Desktop/sample.csv";
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
文件中的列有一个 header,有时在原始数据下方有附加信息(例如字符串或整数单元格中的数字或单词)。
我在之前的问题中询问了如何忽略此附加信息,并得到了此代码作为答案:
List<Country> list = new ArrayList<Country>();
String [] row = csvReader.readNext(); //skip header
if(row == null) throw new RuntimeException("File is empty");
row = csvReader.readNext();
String [] nextRow = csvReader.readNext();
while(row != null) {
if(nextRow == null) break; //check what 'row' is last
if("Total:".equalsIgnoreCase(row[1])) break; //check column for special strings
list.add(csv.processLine(strat, row)); <----
row = nextRow;
nextRow = csvReader.readNext();
当我尝试实现这段代码时,我在箭头标记的行遇到了两个错误。
Exception in thread "main" java.lang.Error: Unresolved compilation
problems:
The method add(Country) in the type List is not
applicable for the arguments (Object)
The method processLine(MappingStrategy, String[]) from the type CsvToBean is not visible
有人知道如何解决这个问题吗?我是 Java.
的新手
非常感谢。
The method processLine(MappingStrategy, String[]) from the type
CsvToBean is not visible
表示此方法必须是可访问的(可能的原因;它是私有的、受保护的或友好的),所以使用 public
The method add(Country) in the type List is not applicable for the
arguments (Object)
并且必须 return 键入国家/地区,方法签名必须类似于;
public Country processLine(ColumnPositionMappingStrategy strat, String [] row)
一个解决方案应该是向下转换从 processLine 到 Country 的响应,如下所示:
list.add(csv.processLine(strat, row));
到
list.add((国家) csv.processLine(strat, row));
但是你的第二个问题,关于方法可见性,清楚地表明你没有使用库的 public API。
请阅读库的文档和示例,了解如何使用它。
使用 CSVToBeanFilter I pointed out in 它应该可以解决您的问题。
在此代码中,目的是解析 CSV 文件并将其数据映射到 bean object。
ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
strat.setType(Country.class);
String[] columns = new String[] {"countryName", "capital"};
strat.setColumnMapping(columns);
CsvToBean csv = new CsvToBean();
String csvFilename = "C:/Users/user/Desktop/sample.csv";
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
文件中的列有一个 header,有时在原始数据下方有附加信息(例如字符串或整数单元格中的数字或单词)。
我在之前的问题中询问了如何忽略此附加信息,并得到了此代码作为答案:
List<Country> list = new ArrayList<Country>();
String [] row = csvReader.readNext(); //skip header
if(row == null) throw new RuntimeException("File is empty");
row = csvReader.readNext();
String [] nextRow = csvReader.readNext();
while(row != null) {
if(nextRow == null) break; //check what 'row' is last
if("Total:".equalsIgnoreCase(row[1])) break; //check column for special strings
list.add(csv.processLine(strat, row)); <----
row = nextRow;
nextRow = csvReader.readNext();
当我尝试实现这段代码时,我在箭头标记的行遇到了两个错误。
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method add(Country) in the type List is not applicable for the arguments (Object)
The method processLine(MappingStrategy, String[]) from the type CsvToBean is not visible
有人知道如何解决这个问题吗?我是 Java.
的新手非常感谢。
The method processLine(MappingStrategy, String[]) from the type CsvToBean is not visible
表示此方法必须是可访问的(可能的原因;它是私有的、受保护的或友好的),所以使用 public
The method add(Country) in the type List is not applicable for the arguments (Object)
并且必须 return 键入国家/地区,方法签名必须类似于;
public Country processLine(ColumnPositionMappingStrategy strat, String [] row)
一个解决方案应该是向下转换从 processLine 到 Country 的响应,如下所示:
list.add(csv.processLine(strat, row));
到
list.add((国家) csv.processLine(strat, row));
但是你的第二个问题,关于方法可见性,清楚地表明你没有使用库的 public API。
请阅读库的文档和示例,了解如何使用它。
使用 CSVToBeanFilter I pointed out in