List of usage examples for org.apache.commons.io LineIterator closeQuietly
public static void closeQuietly(LineIterator iterator)
From source file:com.google.demo.translate.Translator.java
public static void main(String[] args) { parseInputs();/* w w w. java 2s . c om*/ try { String headers = String.join(",", source, targets.stream().map(i -> i.toString()).collect(Collectors.joining(","))); Files.write(output, Arrays.asList(headers), UTF_8, APPEND, CREATE); List<String> texts = new ArrayList<>(); while (it.hasNext()) { texts.add(preTranslationParser(it.next())); if (texts.size() == 10 || !it.hasNext()) { translate(texts); texts = new ArrayList<>(); } } } catch (IOException e) { throw new RuntimeException(e); } finally { LineIterator.closeQuietly(it); } }
From source file:eu.annocultor.converters.geonames.GeonamesDumpToRdf.java
public static void main(String[] args) throws Exception { File root = new File("input_source"); // load country-continent match countryToContinent// w w w . jav a 2s. c om .load((new GeonamesDumpToRdf()).getClass().getResourceAsStream("/country-to-continent.properties")); // creating files Map<String, BufferedWriter> files = new HashMap<String, BufferedWriter>(); Map<String, Boolean> started = new HashMap<String, Boolean>(); for (Object string : countryToContinent.keySet()) { String continent = countryToContinent.getProperty(string.toString()); File dir = new File(root, continent); if (!dir.exists()) { dir.mkdir(); } files.put(string.toString(), new BufferedWriter(new OutputStreamWriter( new FileOutputStream(new File(root, continent + "/" + string + ".rdf")), "UTF-8"))); System.out.println(continent + "/" + string + ".rdf"); started.put(string.toString(), false); } System.out.println(started); Pattern countryPattern = Pattern .compile("<inCountry rdf\\:resource\\=\"http\\://www\\.geonames\\.org/countries/\\#(\\w\\w)\"/>"); long counter = 0; LineIterator it = FileUtils.lineIterator(new File(root, "all-geonames-rdf.txt"), "UTF-8"); try { while (it.hasNext()) { String text = it.nextLine(); if (text.startsWith("http://sws.geonames")) continue; // progress counter++; if (counter % 100000 == 0) { System.out.print("*"); } // System.out.println(counter); // get country String country = null; Matcher matcher = countryPattern.matcher(text); if (matcher.find()) { country = matcher.group(1); } // System.out.println(country); if (country == null) country = "null"; text = text.replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><rdf:RDF", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><rdf:RDF"); if (started.get(country) == null) throw new Exception("Unknow country " + country); if (started.get(country).booleanValue()) { // remove RDF opening text = text.substring(text.indexOf("<rdf:RDF ")); text = text.substring(text.indexOf(">") + 1); } // remove RDF ending text = text.substring(0, text.indexOf("</rdf:RDF>")); files.get(country).append(text + "\n"); if (!started.get(country).booleanValue()) { // System.out.println("Started with country " + country); } started.put(country, true); } } finally { LineIterator.closeQuietly(it); } for (Object string : countryToContinent.keySet()) { boolean hasStarted = started.get(string.toString()).booleanValue(); if (hasStarted) { BufferedWriter bf = files.get(string.toString()); bf.append("</rdf:RDF>"); bf.flush(); bf.close(); } } return; }
From source file:com.icantrap.collections.dawg.TrieValidationTest.java
@Before public void before() throws IOException { assumeThat(System.getProperty("RUN_VALIDATION"), is("on")); LineIterator iter = IOUtils.lineIterator(getClass().getResourceAsStream("/TWL06.txt"), null); dawgBuilder = new DawgBuilder(); while (iter.hasNext()) dawgBuilder.add(iter.next());/*from w ww . j a v a 2 s . co m*/ LineIterator.closeQuietly(iter); System.out.println("Uncompressed: " + dawgBuilder.nodeCount() + " nodes"); StopWatch stopWatch = new StopWatch(); stopWatch.start(); dawgBuilder.build(); stopWatch.stop(); System.out.println("Time to compress: " + stopWatch.getTime() + " ms."); System.out.println("Compressed: " + dawgBuilder.nodeCount() + " nodes"); }
From source file:edu.umd.cs.hcil.twitterreplay.GzippedFileReader.java
public void close() { LineIterator.closeQuietly(mReaderIterator); }
From source file:com.comcast.cats.monitor.util.FileSearchUtil.java
public static Integer countHitsByRegex(String filePath, String expression) throws IOException { int hits = 0; if ((null == expression) || (expression.isEmpty()) || (null == filePath) || (filePath.isEmpty())) { throw new IllegalArgumentException("Expression/FilePath is NULL of EMPTY !!!"); } else {/*w w w . j a v a 2 s . c o m*/ File file = new File(filePath); Pattern pattern = Pattern.compile(expression); if (file.exists()) { LineIterator lineIterator = FileUtils.lineIterator(file, UTF_8_ENCODING); LOGGER.info("Expression under search = " + expression); try { while (lineIterator.hasNext()) { String line = lineIterator.nextLine(); if (pattern.matcher(line).find()) { hits++; } } } finally { LineIterator.closeQuietly(lineIterator); } } } return hits; }
From source file:cs.rsa.ts14.standard.TimesagEngine.java
public String getTimesagReport(File file, TimesagLineProcessor tlp) throws IOException { // Create an iterator for the lines in the file LineIterator it = FileUtils.lineIterator(file, "UTF-8"); tlp.beginProcess();/*from w w w . j av a 2s .c o m*/ try { while (it.hasNext()) { // process each line String line = it.nextLine(); LineType linetype = tlp.process(line); if (linetype == LineType.INVALID_LINE) { // todo throw exception ? break; } } } finally { LineIterator.closeQuietly(it); } tlp.endProcess(); if (!tlp.lastError().equals("No error")) { return ("Error in input: " + tlp.lastError()); } else { return (tlp.getReport()); } }
From source file:com.polytech4A.cuttingstock.core.resolution.util.context.ContextLoaderUtils.java
/** * Load the content of the context file. * * @param path path of the context File. * @return Context loaded./* w w w. j a va 2s. co m*/ * @throws IOException if file not found. * @throws MalformedContextFileException if the Context file don't have the right structure. * @throws IllogicalContextException if an image is bigger than pattern max size. */ public static Context loadContext(String path) throws IOException, MalformedContextFileException, IllogicalContextException { File file = new File(path); LineIterator it = FileUtils.lineIterator(file, "UTF-8"); ArrayList<Box> boxes = new ArrayList<>(); try { Double x = loadLine(it, "^LX=[0-9]{1,13}(\\.[0-9]*)?$"); Double y = loadLine(it, "LY=[0-9]{1,13}(\\.[0-9]*)?"); int cost = loadLine(it, "m=[0-9]{1,13}(\\.[0-9]*)?").intValue(); while (it.hasNext()) { boxes.add(loadBox(it.nextLine())); } LineIterator.closeQuietly(it); double max = Math.max(x, y); if (boxes.parallelStream().anyMatch(b -> b.getSize().getX() > max) || boxes.parallelStream().anyMatch(b -> b.getSize().getY() > max)) { throw new IllogicalContextException("There is an image which is bigger than the pattern."); } return new Context(file.getName(), 20, 1, boxes, new Vector(x, y)); } catch (MalformedContextFileException mctx) { throw mctx; } finally { LineIterator.closeQuietly(it); } }
From source file:fr.ericlab.util.Util.java
static public LinkedList<String> readStopWords(String pathToStopwordsFile) { LinkedList<String> stopWords = new LinkedList<>(); if (pathToStopwordsFile != null) { LineIterator it = null;/*from www .j av a 2s . co m*/ try { it = FileUtils.lineIterator(new File(pathToStopwordsFile), "UTF-8"); while (it.hasNext()) { stopWords.add(it.nextLine()); } } catch (IOException ex) { Logger.getLogger(MABED.class.getName()).log(Level.SEVERE, null, ex); } finally { LineIterator.closeQuietly(it); } } return stopWords; }
From source file:com.icantrap.collections.dawg.DawgValidationTest.java
@Test public void containsAllWords() throws IOException { LineIterator iter = IOUtils.lineIterator(getClass().getResourceAsStream("/TWL06.txt"), null); StopWatch stopWatch = new StopWatch(); stopWatch.start();/*from w w w . j a v a 2s . c o m*/ while (iter.hasNext()) { String word = iter.next(); assertTrue("Missing word (" + word + ")", dawg.contains(word)); } stopWatch.stop(); System.out.println("Time to query: " + stopWatch.getTime() + " ms."); LineIterator.closeQuietly(iter); }
From source file:com.icantrap.collections.dawg.TrieValidationTest.java
@Test public void containsAllWords() throws IOException { LineIterator iter = IOUtils.lineIterator(getClass().getResourceAsStream("/TWL06.txt"), null); StopWatch stopWatch = new StopWatch(); stopWatch.start();// w w w. j a va 2 s. co m while (iter.hasNext()) { String word = iter.next(); assertTrue("Missing word (" + word + ")", dawgBuilder.contains(word)); } stopWatch.stop(); System.out.println("Time to query: " + stopWatch.getTime() + " ms."); LineIterator.closeQuietly(iter); }