FixedWidthReader extends FileReader implements DataReader<FixedWidthReadOptions> { public Table read(FixedWidthReadOptions options) throws IOException { return read(options, false); } FixedWidthReader(); static void register(ReaderRegistry registry); Table read(FixedWidthReadOptions options); String printColumnTypes(FixedWidthReadOptions options); ColumnType[] detectColumnTypes(Reader reader, FixedWidthReadOptions options); @Override Table read(Source source);  }
@Test public void testWithCarsData() throws Exception { Table table = Table.read() .usingOptions( FixedWidthReadOptions.builder("../data/fixed_width_cars_test.txt") .header(true) .columnTypes(car_types) .columnSpecs(car_fields_specs) .padding('_') .systemLineEnding() .build()); String[] expected = new String[]{"Year", "Make", "Model", "Description", "Price"}; assertArrayEquals(expected, table.columnNames().toArray()); table = table.sortDescendingOn("Year"); table.removeColumns("Description"); expected = new String[]{"Year", "Make", "Model", "Price"}; assertArrayEquals(expected, table.columnNames().toArray()); }
@Test public void testWithColumnSKIP() throws Exception { Table table = Table.read() .usingOptions( FixedWidthReadOptions.builder("../data/fixed_width_cars_test.txt") .header(true) .columnTypes(car_types_with_SKIP) .columnSpecs(car_fields_specs) .padding('_') .systemLineEnding() .build()); assertEquals(4, table.columnCount()); String[] expected = new String[]{"Year", "Make", "Model", "Price"}; assertArrayEquals(expected, table.columnNames().toArray()); }
@Test public void testWithColumnSKIPWithoutHeader() throws Exception { Table table = Table.read() .usingOptions( FixedWidthReadOptions.builder("../data/fixed_width_cars_no_header_test.txt") .header(false) .columnTypes(car_types_with_SKIP) .columnSpecs(car_fields_specs) .padding('_') .systemLineEnding() .skipTrailingCharsUntilNewline(true) .build()); assertEquals(4, table.columnCount()); String[] expected = new String[]{"C0", "C1", "C2", "C4"}; assertArrayEquals(expected, table.columnNames().toArray()); }
@Test public void testWithMissingValue() throws Exception { Reader reader = new FileReader("../data/fixed_width_missing_values.txt"); FixedWidthReadOptions options = FixedWidthReadOptions.builder(reader) .header(true) .columnSpecs(car_fields_specs) .padding('_') .systemLineEnding() .missingValueIndicator("null") .minimizeColumnSizes() .sample(false) .build(); Table t = Table.read().usingOptions(options); assertEquals(2, t.shortColumn(0).countMissing()); assertEquals(2, t.stringColumn(1).countMissing()); assertEquals(1, t.stringColumn(2).countMissing()); assertEquals(3, t.stringColumn(3).countMissing()); }
@Test public void testWithSkipTrailingCharsUntilNewline() throws Exception { Table table = Table.read() .usingOptions( FixedWidthReadOptions.builder("../data/fixed_width_wrong_line_length.txt") .header(true) .columnTypes(car_types) .columnSpecs(car_fields_specs) .padding('_') .systemLineEnding() .skipTrailingCharsUntilNewline(true) .build()); String[] expected = new String[]{"Year", "Make", "Model", "Description", "Price"}; assertArrayEquals(expected, table.columnNames().toArray()); table = table.sortDescendingOn("Year"); table.removeColumns("Price"); expected = new String[]{"Year", "Make", "Model", "Description"}; assertArrayEquals(expected, table.columnNames().toArray()); }