Home > OS >  OpenCSV with Java spring boot can't read values of first column
OpenCSV with Java spring boot can't read values of first column

Time:01-19

I am trying to read values from csv file with Java spring boot with dependency of opencsv. But the values of first column are not geting readed while other columns are fine. It returns them as 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   '\''  
                    '}';
    }
}

And the row of csv files look like this:

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

Ive tried something like this:

        BufferedReader br = new BufferedReader(new InputStreamReader(
                    new FileInputStream(path), "UTF-8"));

And also to save the file in different txt editors but no success.

CodePudding user response:

It happens that the file, in the beginning, had something like : ZWNBSP Intelli J editor couldn't see that easily. It seems that it is related to BOM.

Unicode of that is \uFEFF

Fix:

InputStream input = new FileInputStream(path);
        Reader reader = new InputStreamReader(new BOMInputStream(input), StandardCharsets.UTF_8);
  • Related