reading the fixed format csv

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Arrays;

public class CSVSplitter {

    public static void main(String[] args) throws Exception {
        String inputFileName = "path/to/input.csv"; // Update this to your input file path
        String outputFileName = "path/to/output.csv"; // Update this to your output file path

        try (
                Reader in = new FileReader(inputFileName);
                Writer out = new FileWriter(outputFileName);
                CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT.withHeader("lastname", "firstname", "middlename", "gender", "nationality"))
        ) {
            Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in);
            for (CSVRecord record : records) {
                String fullText = record.get(0);
                String[] extractedValues = extractValues(fullText);

                printer.printRecord((Object[]) extractedValues);
            }
        }
    }

    private static String[] extractValues(String text) {
        // Assuming you want to extract values at character positions: 6, 12, 22, 26, and 32
        // And assuming the first character position is 1 (not 0 as in arrays)
        String lastName = text.substring(0, 6).trim();
        String firstName = text.substring(6, 12).trim();
        String middleName = text.substring(12, 22).trim();
        String gender = text.substring(22, 26).trim();
        String nationality = text.substring(26, 32).trim();

        // Adjust the above indices according to your exact requirements
        return new String[]{lastName, firstName, middleName, gender, nationality};
    }
}

Bean class

import com.opencsv.bean.CsvBindByPosition;
import com.opencsv.bean.CsvCustomBindByPosition;
import com.opencsv.bean.AbstractBeanField;
import com.opencsv.exceptions.CsvDataTypeMismatchException;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;

public class PersonRecord {
    @CsvCustomBindByPosition(position = 0, converter = CustomConverter.class)
    private String lastName;
    private String firstName;
    private String middleName;
    private String gender;
    private String nationality;

    // Getters and Setters
    public String getLastName() { return lastName; }
    public void setLastName(String lastName) { this.lastName = lastName; }

    public String getFirstName() { return firstName; }
    public void setFirstName(String firstName) { this.firstName = firstName; }

    public String getMiddleName() { return middleName; }
    public void setMiddleName(String middleName) { this.middleName = middleName; }

    public String getGender() { return gender; }
    public void setGender(String gender) { this.gender = gender; }

    public String getNationality() { return nationality; }
    public void setNationality(String nationality) { this.nationality = nationality; }
}

class CustomConverter extends AbstractBeanField<String, Integer> {
    @Override
    protected Object convert(String value) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException {
        PersonRecord record = new PersonRecord();
        record.setLastName(value.substring(0, 6).trim());
        record.setFirstName(value.substring(6, 12).trim());
        record.setMiddleName(value.substring(12, 22).trim());
        record.setGender(value.substring(22, 26).trim());
        record.setNationality(value.substring(26, 32).trim());
        return record;
    }
}

Main Class to Read CSV

import com.opencsv.bean.CsvToBeanBuilder;
import java.io.FileReader;
import java.util.List;

public class CSVReaderWithBean {
    public static void main(String[] args) {
        String inputFileName = "path/to/input.csv"; // Update this to your input file path
        try {
            List<PersonRecord> beans = new CsvToBeanBuilder<PersonRecord>(new FileReader(inputFileName))
                    .withType(PersonRecord.class)
                    .build()
                    .parse();

            for (PersonRecord record : beans) {
                System.out.println(record.getLastName() + " " + record.getFirstName() + " " + record.getMiddleName() + " " + record.getGender() + " " + record.getNationality());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}