BaseFixedLengthImporter extends BaseTextImporter { protected List<String[]> readFixedLengthFile(byte[] bytes, List<Integer> fieldLengths) { try (BufferedReader reader = new BufferedReader( new CharSequenceReader(new String(bytes, StandardCharsets.UTF_8)))) { List<String[]> result = new ArrayList<>(); String line = reader.readLine(); while (line != null) { List<String> temp = new ArrayList<>(); int start = 0; for (Integer len : fieldLengths) { if (start + len <= line.length()) { String field = line.substring(start, start + len); temp.add(field.trim()); } else if (start <= line.length()) { String field = line.substring(start, line.length()); temp.add(field.trim()); } start += len; } result.add(temp.toArray(new String[0])); line = reader.readLine(); } return result; } catch (IOException ex) { throw new OCSImportException(ex.getMessage(), ex); } }  @Override int countRows(byte[] bytes, int sheetIndex);  }
@Test public void testReadFile() throws IOException { byte[] bytes = readFile("importertest.fixed"); List<String[]> lines = importer.readFixedLengthFile(bytes, Lists.newArrayList(10, 10, 10, 10, 10, 10, 10, 10,10)); PersonDTO dto = importer.processRow(0, lines.get(0), PersonDTO.class); assertNotNull(dto); assertEquals("Bas", dto.getName()); assertEquals(1, dto.getNumber().intValue()); assertEquals(2.4, dto.getFactor().doubleValue(), 0.001); assertEquals("abc", dto.getRandom()); assertEquals(Gender.M, dto.getGender()); assertEquals(1.50, dto.getPercentage().doubleValue(), 0.001); assertEquals(DateUtils.createLocalDate("01022016"), dto.getDate()); dto = importer.processRow(1, lines.get(1), PersonDTO.class); assertNotNull(dto); assertEquals("Unknown", dto.getName()); assertEquals(2, dto.getNumber().intValue()); assertEquals(1.0, dto.getFactor().doubleValue(), 0.001); assertEquals(DateUtils.createLocalDate("01022015"), dto.getDate()); dto = importer.processRow(1, lines.get(2), PersonDTO.class); assertNotNull(dto); assertEquals("Endy", dto.getName()); assertEquals(-3, dto.getNumber().intValue()); }