List of usage examples for java.nio.file Files lines
public static Stream<String> lines(Path path) throws IOException
From source file:com.difference.historybook.importer.HistoryRecordJSONSerialization.java
/** * Stream a JSON file of HistoryRecords//from w w w . ja va 2 s . co m * * @param filename filename to read * @return a stream of HistoryRecord objects parsed from file * @throws IOException */ public static Stream<HistoryRecord> parseFile(String filename) throws IOException { return Files.lines(Paths.get(filename)).map(HistoryRecordJSONSerialization::parse); }
From source file:edu.vassar.cs.cmpu331.tvi.Main.java
private static void renumber(String filename) throws IOException { Path file = Paths.get(filename); if (Files.notExists(file)) { System.out.println("File not found: " + filename); return;//from w w w . j a v a2s. c om } System.out.println("Renumbering " + filename); Function<String, String> trim = s -> { int i = s.indexOf(':'); if (i > 0) { return s.substring(i + 1).trim(); } return s.trim(); }; List<String> list = Files.lines(file).filter(line -> !line.startsWith("CODE")).map(line -> trim.apply(line)) .collect(Collectors.toList()); int size = list.size(); PrintWriter writer = new PrintWriter(Files.newBufferedWriter(file)); // PrintStream writer = System.out; writer.println("CODE"); IntStream.range(0, size).forEach(index -> { String line = list.get(index); writer.println(index + ": " + line); }); writer.close(); System.out.println("Done."); }
From source file:se.sawano.java.security.otp.rfc6238.ReferenceDataRepository.java
private static Stream<String> readLines() { try {/* ww w . ja v a 2s . c o m*/ @SuppressWarnings("ConstantConditions") final String file = ReferenceDataRepository.class.getClassLoader() .getResource("reference-data-rfc6238.txt").getFile(); final Path p = new File(file).toPath(); return Files.lines(p); } catch (IOException e) { throw new UncheckedIOException(e); } }
From source file:org.apache.samza.sql.testutil.SqlFileParser.java
public static List<String> parseSqlFile(String fileName) { Validate.notEmpty(fileName, "fileName cannot be empty."); List<String> sqlLines; try {//from www .ja v a 2 s.co m sqlLines = Files.lines(Paths.get(fileName)).collect(Collectors.toList()); } catch (IOException e) { String msg = String.format("Unable to parse the sql file %s", fileName); LOG.error(msg, e); throw new SamzaException(msg, e); } List<String> sqlStmts = new ArrayList<>(); String lastStatement = ""; for (String sqlLine : sqlLines) { String sql = sqlLine.trim(); if (sql.toLowerCase().startsWith(INSERT_CMD)) { if (StringUtils.isNotEmpty(lastStatement)) { sqlStmts.add(lastStatement); } lastStatement = sql; } else if (StringUtils.isNotBlank(sql) && !sql.startsWith(SQL_COMMENT_PREFIX)) { lastStatement = String.format("%s %s", lastStatement, sql); } } if (!StringUtils.isWhitespace(lastStatement)) { sqlStmts.add(lastStatement); } return sqlStmts; }
From source file:net.kemitix.checkstyle.ruleset.builder.DefaultRuleReadmeLoader.java
@Override public Stream<String> load(final Rule rule) { if (rule.isEnabled()) { final Path resolve = templateProperties.getReadmeFragments().resolve(rule.getName() + ".md"); log.info("Loading fragment: {}", resolve); try {/*w w w . j a va 2s . c o m*/ return Stream.concat(Stream.of(formatRuleHeader(rule)), Files.lines(resolve)); } catch (IOException e) { throw new ReadmeFragmentNotFoundException(rule.getName(), e); } } else { return Stream.of(formatRuleHeader(rule), "", rule.getReason()); } }
From source file:org.apache.geode.BundledJarsJUnitTest.java
@Before public void loadExpectedJars() throws IOException { String expectedJarFile = TestUtil.getResourcePath(BundledJarsJUnitTest.class, "/expected_jars.txt"); expectedJars = Files.lines(Paths.get(expectedJarFile)).collect(Collectors.toSet()); }
From source file:org.hawkular.alerts.actions.webhook.WebHooks.java
public static synchronized void loadFile() throws IOException { File f = new File(instance.webhooksFile); Path path = f.toPath();/*from w w w.ja v a2 s .c om*/ StringBuilder fullFile = new StringBuilder(); Files.lines(path).forEach(s -> fullFile.append(s)); log.debug("Reading webhooks... " + fullFile.toString()); instance.webhooks = instance.mapper.readValue(fullFile.toString(), Map.class); }
From source file:alliance.docs.DocumentationTest.java
@Test public void testDocumentationIncludes() throws IOException, URISyntaxException { Stream<Path> docs = Files.list(getPath()).filter(f -> f.toString().endsWith(HTML_DIRECTORY)); assertThat("Unresolved directive, FreeMarker reference or broken image found.", docs.noneMatch(f -> { try (Stream<String> lines = Files.lines(f)) { return lines.anyMatch(s -> s.contains(UNRESOLVED_DIRECTORY_MSG) || s.toLowerCase().contains(FREEMARKER_MSG) || s.contains(BASE64_MISSING)); } catch (IOException e) { throw new RuntimeException(e); }//from w w w. j ava 2 s.co m })); }
From source file:com.crossover.trial.weather.util.AirportLoader.java
@Override public void run(ApplicationArguments args) throws Exception { if (args.containsOption("airports") && !args.getOptionValues("airports").isEmpty()) { File dataFile = new File(args.getOptionValues("airports").get(0)); if (dataFile.canRead() && dataFile.isFile() && dataFile.exists()) { log.info("Loading airports from [{}]", dataFile); try (Stream<String> lines = Files.lines(dataFile.toPath())) { lines.map(Row::new).filter(Row::expectedTotalColumns).filter(Row::expectedColumnsFilled) .map(Row::parse).filter(airport -> airport != null).forEach(airportRepository::save); }//from w w w . j a v a2 s . c o m } else log.error("Specified airports data file is not readable [{}]", dataFile.toPath()); } }
From source file:com.schnobosoft.semeval.cortical.Util.java
/** * Read a file that contains one score per line, as a SemEval gold {@code .gs} file. Empty lines * are allowed and are read as missing values. * * @param scoresFile the scores file to read * @return a list of optional double values of the same length as the input file. For an empty * line, a {@link Optional#EMPTY} object is added to the output list. * @throws IOException//from w ww .j a va 2s . com */ public static List<Optional> readScoresFile(File scoresFile) throws IOException { if (!scoresFile.getName().startsWith(GS_FILE_PREFIX)) { throw new IllegalArgumentException(scoresFile + " does not match expected pattern."); } LOG.info("Reading scores file " + scoresFile); return Files.lines(scoresFile.toPath()) .map(line -> line.isEmpty() ? Optional.empty() : Optional.of(Double.valueOf(line))) .collect(Collectors.toList()); }