private void readConfigFile(String filename) throws IOException { ReaderLineIterator iterator = new ReaderLineIterator(IOUtil.getReaderForURI(filename)); try { while (iterator.hasNext()) { String className = iterator.next(); registerConverterClass((Class<? extends Converter>) Class.forName(className)); } } catch (ClassNotFoundException e) { throw new ConfigurationError(e); } finally { iterator.close(); } }
@Override public String next() { String result = next; fetchNext(); return result; }
public static int runShellCommands(ReaderLineIterator iterator, Writer outputWriter, ErrorHandler errorHandler) { int result = 0; while (iterator.hasNext()) { String command = iterator.next().trim(); if (command.length() > 0) result = runShellCommand(command, outputWriter, errorHandler); } return result; }
public static void printFile(String uri) throws IOException { ReaderLineIterator iterator = null; try { iterator = new ReaderLineIterator(IOUtil.getReaderForURI(uri)); while (iterator.hasNext()) System.out.println(iterator.next()); } finally { IOUtil.close(iterator); } }
private static DBExecutionResult runScript( Reader reader, char separator, Connection connection, boolean ignoreComments, ErrorHandler errorHandler) { ReaderLineIterator iterator = new ReaderLineIterator(reader); SQLScriptException exception = null; Object result = null; try { StringBuilder cmd = new StringBuilder(); while (iterator.hasNext()) { String line = iterator.next().trim(); if (line.startsWith("--")) continue; cmd.append(line); boolean lineEndsWithSeparator = (line.length() > 0 && StringUtil.lastChar(line) == separator); if (lineEndsWithSeparator || !iterator.hasNext()) { if (lineEndsWithSeparator) cmd.delete(cmd.length() - 1, cmd.length()); // delete trailing separators exception = new SQLScriptException(e, iterator.lineCount());
public static void main(String[] args) throws IOException { // creates a line based reader for the input file Reader reader = new FileReader(FILE_NAME); ReaderLineIterator src = new ReaderLineIterator(reader); // sets up a filtered iterator that uses the upper iterator as source Filter<String> filter = new LineFilter(); FilteringIterator<String> iterator = new FilteringIterator<String>(src, filter); // create a writer to save the rows that matched the filter Writer out = new BufferedWriter(new FileWriter("matches.csv")); // initialize counter and timer int matchCount = 0; System.out.println("Running..."); long startMillis = System.currentTimeMillis(); // iterate the entries while (iterator.hasNext()) { String row = iterator.next(); out.write(row); matchCount++; } out.close(); reader.close(); // output counter and timer values long elapsedTime = System.currentTimeMillis() - startMillis; System.out.println("Processed file " + FILE_NAME + " with " + src.lineCount() + " entries " + "within " + elapsedTime + "ms (" + (src.lineCount() * 1000L / elapsedTime) + " entries per second)"); System.out.println("Found " + matchCount + " matches"); }
@Override public HeavyweightIterator<String> iterator() { try { return new ReaderLineIterator(IOUtil.getReaderForURI(uri)); } catch (IOException e) { throw new RuntimeException("Unable to create an Iterator for URI '" + uri + "'", e); } }
/** * Closes the source */ public void close() { if (lineIterator != null) lineIterator.close(); lineIterator = null; }
private String[] fetchNextLine() { try { if (lineIterator == null) return null; String line = null; boolean success = false; // fetch next appropriate lines skipping empty lines if they shall be ignored while (lineIterator.hasNext()) { lineCount++; line = lineIterator.next(); if ((line.length() > 0 || !ignoreEmptyLines) && (lineFilter == null || lineFilter.matcher(line).matches())) { success = true; break; } } if (!success) { close(); return null; } else if (StringUtil.isEmpty(line)) { return new String[0]; } else { return parser.parse(line); } } catch (ParseException e) { throw new RuntimeException("Unexpected error. ", e); } }
public static void main(String[] args) throws IOException { // creates a line based reader for the input file Reader reader = new FileReader(FILE_NAME); ReaderLineIterator src = new ReaderLineIterator(reader); // sets up a filtered iterator that uses the upper iterator as source Filter<String> filter = new LineFilter(); FilteringIterator<String> iterator = new FilteringIterator<String>(src, filter); // create a writer to save the rows that matched the filter Writer out = new BufferedWriter(new FileWriter("matches.csv")); // initialize counter and timer int matchCount = 0; System.out.println("Running..."); long startMillis = System.currentTimeMillis(); // iterate the entries while (iterator.hasNext()) { String row = iterator.next(); out.write(row); matchCount++; } out.close(); reader.close(); // output counter and timer values long elapsedTime = System.currentTimeMillis() - startMillis; System.out.println("Processed file " + FILE_NAME + " with " + src.lineCount() + " entries " + "within " + elapsedTime + "ms (" + (src.lineCount() * 1000L / elapsedTime) + " entries per second)"); System.out.println("Found " + matchCount + " matches"); }
public FixedWidthLineIterator(Reader reader, PadFormat[] formats, boolean ignoreEmptyLines, String lineFilter) { this.lineIterator = new ReaderLineIterator(reader); parser = new FixedWidthLineParser(formats); this.ignoreEmptyLines = ignoreEmptyLines; this.lineCount = 0; this.lineFilter = (lineFilter != null ? Pattern.compile(lineFilter) : null); }
/** Closes the source */ @Override public void close() { if (lineIterator != null) lineIterator.close(); lineIterator = null; }
try { reader = IOUtil.getReaderForURI(filename, encoding); iterator = new ReaderLineIterator(reader); String key = null; String value = ""; while (iterator.hasNext()) { String line = iterator.next(); line = line.trim(); if (line.startsWith("#")) iterator.close(); else IOUtil.close(reader);
private String[] fetchNextLine() { try { if (lineIterator == null) return null; String line = null; boolean success = false; // fetch next appropriate lines skipping empty lines if they shall be ignored while (lineIterator.hasNext()) { lineCount++; line = lineIterator.next(); if ((line.length() > 0 || !ignoreEmptyLines) && (lineFilter == null || lineFilter.matcher(line).matches())) { success = true; break; } } if (!success) { close(); return null; } else if (StringUtil.isEmpty(line)) { return new String[0]; } else { return parser.parse(line); } } catch (ParseException e) { throw new RuntimeException("Unexpected error. ", e); } }
public FixedWidthLineIterator(Reader reader, PadFormat[] formats, boolean ignoreEmptyLines, String lineFilter) { this.lineIterator = new ReaderLineIterator(reader); parser = new FixedWidthLineParser(formats); this.ignoreEmptyLines = ignoreEmptyLines; this.lineCount = 0; this.lineFilter = (lineFilter != null ? Pattern.compile(lineFilter) : null); }
private void fetchNext() { try { if (reader != null) { do { next = reader.readLine(); } while (skipEmptyLines && next != null && next.trim().length() == 0); if (next == null) close(); lineCount++; } else { next = null; } } catch (IOException e) { throw new RuntimeException(e); } }
public ReaderLineIterator(Reader reader, boolean skipEmptyLines) { if (reader instanceof BufferedReader) this.reader = (BufferedReader) reader; else this.reader = new BufferedReader(reader); this.skipEmptyLines = skipEmptyLines; this.lineCount = 0; fetchNext(); }