我正试图从Java spring boot与opencsv依赖的csv文件中读取值。但是第一列的值没有被读取,而其他列是正常的。返回null
Reader reader = Files.newBufferedReader(Paths.get(path));
CsvToBean<CsvBikeTrips> csvToBean = new CsvToBeanBuilder(reader)
.withType(CsvBikeTrips.class)
.withIgnoreLeadingWhiteSpace(false)
.build();
List<CsvBikeTrips> csvBikeTrips = csvToBean.parse();
public class CsvBikeTrips {
@CsvBindByName(column = "Departure")
private String departureTimestamp;
@CsvBindByName(column = "Return")
private String returnTimestamp;
@CsvBindByName(column = "Departure station id")
private String departureStationId;
@CsvBindByName(column = "Return station id")
private String returnStationId;
@CsvBindByName(column = "Departure station name")
private String departureStationName;
@CsvBindByName(column = "Return station name")
private String returnStationName;
@CsvBindByName(column = "Covered distance (m)")
private String coveredDistance;
@CsvBindByName(column = "Duration (sec.)")
private String duration;
public String getDepartureTimestamp() {
return departureTimestamp;
}
public void setDepartureTimestamp(String departureTimestamp) {
this.departureTimestamp = departureTimestamp;
}
public String getReturnTimestamp() {
return returnTimestamp;
}
public void setReturnTimestamp(String returnTimestamp) {
this.returnTimestamp = returnTimestamp;
}
public String getDepartureStationId() {
return departureStationId;
}
public void setDepartureStationId(String departureStationId) {
this.departureStationId = departureStationId;
}
public String getReturnStationId() {
return returnStationId;
}
public void setReturnStationId(String returnStationId) {
this.returnStationId = returnStationId;
}
public String getDepartureStationName() {
return departureStationName;
}
public void setDepartureStationName(String departureStationName) {
this.departureStationName = departureStationName;
}
public String getReturnStationName() {
return returnStationName;
}
public void setReturnStationName(String returnStationName) {
this.returnStationName = returnStationName;
}
public String getCoveredDistance() {
return coveredDistance;
}
public void setCoveredDistance(String coveredDistance) {
this.coveredDistance = coveredDistance;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
@Override
public String toString() {
return "CsvBikeTrips{" +
"departureTimestamp='" + departureTimestamp + ''' +
", returnTimestamp='" + returnTimestamp + ''' +
", departureStationId='" + departureStationId + ''' +
", returnStationId='" + returnStationId + ''' +
", departureStationName='" + departureStationName + ''' +
", returnStationName='" + returnStationName + ''' +
", coveredDistance='" + coveredDistance + ''' +
", duration='" + duration + ''' +
'}';
}
}
csv文件的行看起来像这样:
Departure,Return,Departure station id,Departure station name,Return station id,Return station name,Covered distance (m),Duration (sec.)
2021-05-31T23:57:25,2021-06-01T00:05:46,094,Laajalahden aukio,100,Teljäntie,2043,500
2021-05-31T23:56:59,2021-06-01T00:07:14,082,Töölöntulli,113,Pasilan asema,1870,611
我试过这样做:
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(path), "UTF-8"));
还可以将文件保存在不同的文本编辑器中,但没有成功。
在开始的时候,这个文件碰巧有这样的内容:ZWNBSP
Intelli J编辑器看不出来。这似乎与BOM有关。
Unicode是uFEFF
修复:
InputStream input = new FileInputStream(path);
Reader reader = new InputStreamReader(new BOMInputStream(input), StandardCharsets.UTF_8);