List of usage examples for java.nio.file Files lines
public static Stream<String> lines(Path path, Charset cs) throws IOException
From source file:Main.java
public static void main(String[] args) throws Exception { long uniqueWords = Files.lines(Paths.get("Main.java"), Charset.defaultCharset()) .flatMap(line -> Arrays.stream(line.split(" "))).distinct().count(); System.out.println("There are " + uniqueWords + " unique words in data.txt"); }
From source file:org.ingini.mongodb.jongo.example.util.ImportQuotes.java
public static void main(String[] args) throws IOException, URISyntaxException { Jongo jongo = new Jongo(new MongoClient("127.0.0.1", 27017).getDB("movie_db")); MongoCollection quoteCollection = jongo.getCollection("quotes"); URI uri = ImportQuotes.class.getClassLoader().getResource("raw/quotes.list").toURI(); Stream<String> lines = Files.lines(Paths.get(uri), forName("windows-1252")); List<List<String>> quotesList = new ArrayList<>(); lines.skip(14).forEach(s -> {//from www .j a v a2s . c o m List<String> quotes = new ArrayList<>(); if (quotesList.size() == 0) { quotesList.add(quotes); } quotes = quotesList.get(quotesList.size() - 1); if (s.isEmpty() && quotes.get(quotes.size() - 1).isEmpty()) { quotesList.add(new ArrayList<>()); } quotes.add(s); }); System.out.println("Quotes lists: " + quotesList.size()); List<FilmQuote> filmQuotes = new ArrayList<>(); for (List<String> quotes : quotesList) { Set<ActorQuote> actorQuotes = new LinkedHashSet<>(); Iterator<String> iterator = quotes.subList(1, quotes.size()).iterator(); String actorName = null; String quote = null; while (iterator.hasNext()) { String s = iterator.next(); if (s.contains(":")) { if (actorName != null && quote != null) { actorQuotes.add(new ActorQuote(actorName, quote)); } actorName = StringUtils.substringBefore(s, ":"); quote = StringUtils.substringAfter(s, ":"); } else if (s.isEmpty()) { actorQuotes.add(new ActorQuote(actorName, quote)); } else { quote += StringUtils.substringAfter(s, " "); } } FilmQuote filmQuote = new FilmQuote("english", quotes.get(0), actorQuotes); filmQuotes.add(filmQuote); quoteCollection.insert(filmQuote); } System.out.println("Film Quotes lists: " + filmQuotes.size()); }
From source file:com.nextdoor.bender.ValidateSchema.java
public static void main(String[] args) throws ParseException, InterruptedException, IOException { /*/*from ww w.ja v a 2 s. co m*/ * Parse cli arguments */ Options options = new Options(); options.addOption(Option.builder().longOpt("schema").hasArg() .desc("Filename to output schema to. Default: schema.json").build()); options.addOption(Option.builder().longOpt("configs").hasArgs() .desc("List of config files to validate against schema.").build()); CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(options, args); String schemaFilename = cmd.getOptionValue("schema", "schema.json"); String[] configFilenames = cmd.getOptionValues("configs"); /* * Validate config files against schema */ boolean hasFailures = false; for (String configFilename : configFilenames) { StringBuilder sb = new StringBuilder(); Files.lines(Paths.get(configFilename), StandardCharsets.UTF_8).forEach(p -> sb.append(p + "\n")); System.out.println("Attempting to validate " + configFilename); try { ObjectMapper mapper = BenderConfig.getObjectMapper(configFilename); mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); BenderConfig.load(configFilename, sb.toString(), mapper, true); System.out.println("Valid"); BenderConfig config = BenderConfig.load(configFilename, sb.toString()); } catch (ConfigurationException e) { System.out.println("Invalid"); e.printStackTrace(); hasFailures = true; } } if (hasFailures) { System.exit(1); } }
From source file:com.hortonworks.streamline.streams.common.event.EventLogFileReader.java
public Stream<EventInformation> loadEventLogFileAsStream(File eventLogFile) throws IOException { Stream<String> lines = Files.lines(eventLogFile.toPath(), ENCODING_UTF_8); return lines.map(line -> { try {/* ww w . j av a 2s . c om*/ return (EventInformation) objectMapper.readValue(line, new TypeReference<EventInformation>() { }); } catch (IOException e) { throw new RuntimeException(e); } }); }
From source file:org.flywaydb.core.internal.util.plus.MysqlToH2ConverterTests.java
/** * Read file in class path/*w ww. j a v a2 s . co m*/ * * @param path path * @param joiner joiner * @return string in file */ private String readFileInClassPath(String path, String joiner) { try { return Files.lines(Paths.get(new ClassPathResource(path).getURI()), StandardCharsets.UTF_8) .collect(Collectors.joining(joiner)); } catch (IOException e) { throw new IllegalStateException("Failed to read file", e); } }
From source file:org.flywaydb.core.internal.util.XMysqlToH2SqlReplacerTests.java
/** * Read class path file//from w w w.java 2 s . co m * * @param path path * @param joiner joiner * @return string in file */ private String readClassPathFile(String path, String joiner) { try { return Files.lines(Paths.get(new ClassPathResource(path).getURI()), StandardCharsets.UTF_8) .collect(Collectors.joining(joiner)); } catch (IOException e) { throw new IllegalStateException("Failed to read class path file", e); } }
From source file:com.teradata.benchto.driver.loader.AnnotatedQueryParser.java
public Query parseFile(String queryName, Path inputFile) throws IOException { return parseLines(queryName, Files.lines(inputFile, UTF_8).collect(toList())); }
From source file:by.logscanner.LogScanner.java
private void read(File file) throws FileNotFoundException, IOException { DataOutputStream out = new DataOutputStream(baos); SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); Stream<String> lines = Files.lines(file.toPath(), StandardCharsets.UTF_8); for (String temp : (Iterable<String>) lines::iterator) { out.writeUTF((temp.contains(sdf.format(date)) && temp.contains("[" + startupId + "." + requestNo + "]")) ? temp + "\n" : ""); }/*from w ww .j a v a 2s .co m*/ }
From source file:csv.sorting.PrepareWeatherData.java
private static void writeSortedFileByIndices(Path csvFileIn, List<Integer> indices, Path csvFileOut) { try {/*from w ww . j a va 2 s. c o m*/ List<String> csvDataList = new ArrayList<>(); // This is sorting for the dumb (like me). Read the entire CSV file, skipping the first line: try (Stream<String> lines = Files.lines(csvFileIn, StandardCharsets.US_ASCII).skip(1)) { csvDataList = lines.collect(Collectors.toList()); } // Now write the sorted file: try (BufferedWriter writer = Files.newBufferedWriter(csvFileOut)) { for (Integer index : indices) { writer.write(csvDataList.get(index)); writer.newLine(); } } } catch (Exception e) { throw new RuntimeException(e); } }
From source file:net.kemitix.checkstyle.ruleset.builder.ReadmeWriter.java
private String readFile(final Path file) throws IOException { return Files.lines(file, StandardCharsets.UTF_8).collect(Collectors.joining(NEWLINE)); }