### Question:
FixedWidthReader implements Closeable { public String[] readLine() throws IllegalStateException { try { beforeReadLine(); _rowNumber++; return getValues(); } catch (IOException e) { throw new IllegalStateException(e); } } FixedWidthReader(InputStream stream, String charsetName, int fixedValueWidth,
            boolean failOnInconsistentLineWidth); private  FixedWidthReader(BufferedInputStream stream, String charsetName, int fixedValueWidth,
            boolean failOnInconsistentLineWidth);  FixedWidthReader(InputStream stream, String charsetName, int[] valueWidths,
            boolean failOnInconsistentLineWidth);   FixedWidthReader(BufferedInputStream stream, String charsetName, int[] valueWidths,
            boolean failOnInconsistentLineWidth); String[] readLine(); @Override void close();  }### Answer:
@Test public void testBufferedReader1() throws IOException { final File file = new File("src/test/resources/example_simple1.txt"); final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file)); int[] widths = new int[] { 8, 9 }; try (final FixedWidthReader fixedWidthReader = new FixedWidthReader(stream, CHARSET, widths, false)) { final String[] line1 = fixedWidthReader.readLine(); assertEquals("[greeting, greeter]", Arrays.asList(line1).toString()); final String[] line2 = fixedWidthReader.readLine(); assertEquals("[hello, world]", Arrays.asList(line2).toString()); final String[] line3 = fixedWidthReader.readLine(); assertEquals("[hi, there]", Arrays.asList(line3).toString()); } }
@Test public void testBufferedReader2() throws IOException { final File file = new File("src/test/resources/example_simple2.txt"); final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file)); int[] widths = new int[] {1, 8, 9 }; try (final FixedWidthReader fixedWidthReader = new FixedWidthReader(stream, CHARSET, widths, false)) { final String[] line1 = fixedWidthReader.readLine(); assertEquals("[i, greeting, greeter]", Arrays.asList(line1).toString()); final String[] line2 = fixedWidthReader.readLine(); assertEquals("[1, hello, world]", Arrays.asList(line2).toString()); final String[] line3 = fixedWidthReader.readLine(); assertEquals("[2, hi, there]", Arrays.asList(line3).toString()); } }
@Test public void testBufferedReader3() throws IOException { final File file = new File("src/test/resources/example_simple3.txt"); final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file)); try (final FixedWidthReader fixedWidthReader = new FixedWidthReader(stream, CHARSET, 5, false)) { final String[] line1 = fixedWidthReader.readLine(); assertEquals("[hello]", Arrays.asList(line1).toString()); final String[] line2 = fixedWidthReader.readLine(); assertEquals("[world]", Arrays.asList(line2).toString()); final String[] line3 = fixedWidthReader.readLine(); assertEquals("[howdy]", Arrays.asList(line3).toString()); final String[] line4 = fixedWidthReader.readLine(); assertEquals("[ther]", Arrays.asList(line4).toString()); } }
@Test public void testBufferedReaderFailOnInconsistentRows() throws IOException { final File file = new File("src/test/resources/example_simple3.txt"); final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file)); try (final FixedWidthReader fixedWidthReader = new FixedWidthReader(stream, CHARSET, 5, true)) { final String[] line1 = fixedWidthReader.readLine(); assertEquals("[hello]", Arrays.asList(line1).toString()); final String[] line2 = fixedWidthReader.readLine(); assertEquals("[world]", Arrays.asList(line2).toString()); final String[] line3 = fixedWidthReader.readLine(); assertEquals("[howdy]", Arrays.asList(line3).toString()); exception.expect(InconsistentValueWidthException.class); @SuppressWarnings("unused") final String[] line4 = fixedWidthReader.readLine(); } }