List of usage examples for org.apache.commons.io FileUtils lineIterator
public static LineIterator lineIterator(File file, String encoding) throws IOException
File
. From source file:de.tudarmstadt.ukp.dkpro.keyphrases.bookindexing.evaluation.phrasematch.LineReader.java
@Override public List<String> getListOfStrings(JCas jcas) throws AnalysisEngineProcessException { List<String> goldList = new ArrayList<String>(); LineIterator lineIterator;/*from w w w. ja va 2 s .c o m*/ try { lineIterator = FileUtils.lineIterator(new File(getPath(getDocumentBaseName(jcas))), encoding); } catch (IOException e) { throw new AnalysisEngineProcessException(new Throwable(e)); } try { while (lineIterator.hasNext()) { String line = lineIterator.nextLine().trim(); if (!line.isEmpty()) { if (lowercase) line = line.toLowerCase(); goldList.add(line); } } } finally { LineIterator.closeQuietly(lineIterator); } return goldList; }
From source file:ke.co.tawi.babblesms.server.servlet.upload.UploadUtil.java
/** * Checks that an uploaded Contact file is in proper order. * //from w w w . j a v a2 s .c o m * @param file * @return the feedback of having inspected the file, whether it was proper */ protected String inspectContactFile(File file) { String feedback = ContactUpload.UPLOAD_SUCCESS; int count = 1; LineIterator lineIterator = null; try { lineIterator = FileUtils.lineIterator(file, "UTF-8"); String line; String[] rowTokens, phoneTokens, networkTokens; String network; while (lineIterator.hasNext()) { line = lineIterator.nextLine(); rowTokens = StringUtils.split(line, ','); if (rowTokens.length != 3 && line.length() > 0) {// Every row must have 3 columns return ("Invalid format on line " + count + ": " + line); } phoneTokens = StringUtils.split(rowTokens[1], ';'); networkTokens = StringUtils.split(rowTokens[2], ';'); // Check that the number of phone numbers and number of networks match if (phoneTokens.length != networkTokens.length) { return ("Invalid format on line " + count + ": " + line); } // Check that the phone numbers contain only numbers or spaces for (String phone : phoneTokens) { if (!StringUtils.isNumericSpace(phone)) { return ("Invalid number on line " + count + ": " + line); } } // Check to see that only valid networks have been provided for (String s : networkTokens) { network = StringUtils.lowerCase(StringUtils.trimToEmpty(s)); if (!networkList.contains(network)) { return ("Invalid network on line " + count + ": " + line); } } count++; } } catch (IOException e) { logger.error("IOException when inspecting: " + file); logger.error(e); } finally { if (lineIterator != null) { lineIterator.close(); } } return feedback; }
From source file:net.mindengine.blogix.web.routes.DefaultRoutesParser.java
@Override public List<Route> parseRoutes(File file) throws IOException { LineIterator it = FileUtils.lineIterator(file, "UTF-8"); List<Route> routes = new LinkedList<Route>(); Route currentRoute = null;// www.j ava 2s . c o m while (it.hasNext()) { String line = it.nextLine(); if (line.trim().isEmpty()) { currentRoute = null; } else if (line.startsWith(" ")) { loadModelEntryForRoute(currentRoute, line); } else { currentRoute = parseLine(line.trim()); if (currentRoute != null) { routes.add(currentRoute); } } } return routes; }
From source file:com.comcast.cats.monitor.util.FileSearchUtil.java
public static List<String> getLinesByRegex(String filePath, String expression) throws IOException { if ((null == expression) || (expression.isEmpty()) || (null == filePath) || (filePath.isEmpty())) { throw new IllegalArgumentException("Expressions/FilePath is NULL of EMPTY !!!"); }/*from w w w.j a va 2 s. c o m*/ File file = new File(filePath); Pattern pattern = Pattern.compile(expression); List<String> lines = new LinkedList<String>(); if (file.exists()) { LineIterator lineIterator = FileUtils.lineIterator(file, UTF_8_ENCODING); try { while (lineIterator.hasNext()) { String line = lineIterator.nextLine(); if (pattern.matcher(line).find()) { lines.add(line); } } } finally { LineIterator.closeQuietly(lineIterator); } } return lines; }
From source file:croche.maven.plugin.android.manifestv.CopyManifestMojo.java
/** * {@inheritDoc}/*from www .jav a 2s. c o m*/ * @see org.apache.maven.plugin.Mojo#execute() */ public void execute() throws MojoExecutionException, MojoFailureException { File sourceManifest = new File(this.manifestPath); if (!sourceManifest.canRead()) { throw new MojoFailureException("The manifest file: " + this.manifestPath + " set via the manifestPath configuration could not be read."); } File targetDir = new File(this.targetPath); if (!targetDir.canRead() && !targetDir.mkdirs()) { throw new MojoFailureException("The manifest targetPath: " + this.targetPath + " set via the targetPath configuration could not be read."); } File targetManifest = new File(targetDir, sourceManifest.getName()); OutputStream os = null; try { os = new BufferedOutputStream(new FileOutputStream(targetManifest, false)); LineIterator it = FileUtils.lineIterator(sourceManifest, this.encoding); Set<String> propNames = this.session.getExecutionProperties().stringPropertyNames(); Properties props = this.session.getExecutionProperties(); while (it.hasNext()) { String line = it.nextLine(); // substitute any properies if (line.indexOf("${") > 0) { for (String propName : propNames) { line = line.replace("${" + propName + "}", props.getProperty(propName)); if (line.indexOf("${") == -1) { break; } } } // special handling of the manifest version code String origVersionCode = "android:versionCode=\"" + this.replaceVersionCode + "\""; if (line.indexOf(origVersionCode) > -1) { String newVersionCode = this.session.getExecutionProperties() .getProperty("manifestVersionCode"); if (newVersionCode != null && newVersionCode.trim().length() > 0) { line = line.replace(origVersionCode, "android:versionCode=\"" + newVersionCode + "\""); } } // write line out to output file os.write(line.toString().getBytes(this.encoding)); os.write(IOUtils.LINE_SEPARATOR.getBytes(this.encoding)); } } catch (IOException ex) { throw new MojoFailureException( "Failed to iterate over the content of the manifest file: " + sourceManifest.getAbsolutePath(), ex); } finally { if (os != null) { try { os.flush(); } catch (IOException ex) { throw new MojoFailureException("The target manifest targetManifest: " + targetManifest.getAbsolutePath() + " could not written to due to an io error.", ex); } finally { IOUtils.closeQuietly(os); } } } }
From source file:fr.inria.maestro.lga.graph.model.impl.nodenamer.NodeNamerImpl.java
/** * List of lines are generated from a given file. then this list is used to generate a NodeNamerImpl * * @param file//from w ww . ja va 2s . c om * @return INodeNamer * @throws IOException */ public static INodeNamer load(final File file) throws IOException { final int numNodes = countLines(file, Encoding.DEFAULT_CHARSET_NAME); final Int2ObjectOpenHashMap<String> id2Label = new Int2ObjectOpenHashMap<String>(numNodes); final Object2IntOpenHashMap<String> label2Id = new Object2IntOpenHashMap<String>(numNodes); final LineIterator it = FileUtils.lineIterator(file, Encoding.DEFAULT_CHARSET_NAME); try { int curId = 0; while (it.hasNext()) { final String nodeName = it.next(); id2Label.put(curId, nodeName); label2Id.put(nodeName, curId); curId++; } return new NodeNamerImpl(id2Label, label2Id); } finally { it.close(); } }
From source file:at.sti2.spark.streamer.SparkStreamer.java
private void stream(File fileToStream) { PrintWriter streamWriter = null; LineIterator lineIterator = null;//w ww . ja v a 2 s . c o m long Counter = 0; try { sock = new Socket("localhost", port); } catch (IOException e) { logger.debug("Cannot connect to server."); System.exit(1); } logger.info("Connected."); try { streamWriter = new PrintWriter(sock.getOutputStream()); lineIterator = FileUtils.lineIterator(fileToStream, "UTF-8"); logger.info("Beginning to stream."); Date startStreaming = new Date(); String line = null; while (lineIterator.hasNext()) { line = lineIterator.nextLine(); streamWriter.println(line); Counter++; // if (tripleCounter%1000 == 0){ // long currentTimepoint = (new Date()).getTime(); // System.out.println("Processing " + (1000/(currentTimepoint - timepoint)) + " triples/sec."); // timepoint = currentTimepoint; // streamWriter.flush(); // } } streamWriter.flush(); Date endStreaming = new Date(); logger.info("End of streaming."); logger.info("Streamed " + Counter + " triples/lines."); logger.info("Total streaming time " + (endStreaming.getTime() - startStreaming.getTime()) + " ms."); } catch (IOException e) { logger.error(e.getMessage()); } finally { IOUtils.closeQuietly(streamWriter); lineIterator.close(); logger.info("Disconnected."); } }
From source file:de.tudarmstadt.ukp.experiments.dip.wp1.documents.Step1PrepareContainers.java
/** * Parses the file with relevance/non-relevance information to each query, i.e. * <pre>// w w w .j av a 2 s . com * QID: 1021 * Query: School dealing with bullying. * 1 Physical harm that occurs in an educational setting, performed intentionally by kids to other kids. * 1 Sexual harm that occurs in an educational setting, performed intentionally by kids to other kids. * 0 Bullying outside school. * 0 Bullying by adults. * * QID: 1022 * Query: Signs of bullied kids. * 1 How to identify kids suffering from bulling by other kids. * [...] * </pre> * * @param file file * @return map (queryID, (0 = non-relevant list, 1 = relevant list)) * @throws IOException exception */ public static Map<Integer, Map<Integer, List<String>>> parseRelevantInformationFile(File file) throws IOException { LineIterator lineIterator = FileUtils.lineIterator(file, "utf-8"); Map<Integer, Map<Integer, List<String>>> result = new HashMap<>(); int currentQueryId = Integer.MIN_VALUE; while (lineIterator.hasNext()) { String line = lineIterator.next().trim(); if (line.isEmpty()) { currentQueryId = Integer.MIN_VALUE; } // new query id if (line.startsWith("QID: ")) { currentQueryId = Integer.valueOf(line.split("\\s", 2)[1]); // add to the result if (!result.containsKey(currentQueryId)) { result.put(currentQueryId, new HashMap<>()); // and two empty lists result.get(currentQueryId).put(0, new ArrayList<>()); result.get(currentQueryId).put(1, new ArrayList<>()); } } if (line.startsWith("0 ")) { String nonRelevantInformation = line.split("\\s", 2)[1]; if (currentQueryId == Integer.MIN_VALUE) { throw new IOException("Input file has a wrong format, no QUI provided"); } // add to the result result.get(currentQueryId).get(0).add(nonRelevantInformation); } if (line.startsWith("1 ")) { String relevantInformation = line.split("\\s", 2)[1]; if (currentQueryId == Integer.MIN_VALUE) { throw new IOException("Input file has a wrong format, no QUI provided"); } // add to the result result.get(currentQueryId).get(1).add(relevantInformation); } } // debug for (Map.Entry<Integer, Map<Integer, List<String>>> entry : result.entrySet()) { System.out.println("QID: " + entry.getKey()); for (Map.Entry<Integer, List<String>> innerEntry : entry.getValue().entrySet()) { for (String info : innerEntry.getValue()) { System.out.printf("%d %s%n", innerEntry.getKey(), info); } } System.out.println(); } return result; }
From source file:de.tudarmstadt.ukp.experiments.ej.bills.experiment1.Exp1Reader.java
@Override public void initialize(UimaContext context) throws ResourceInitializationException { super.initialize(context); // open JSON file here try {//from www.j a v a 2s . c o m jsonSize = FileUtils.readLines(new File(jsonFile)).size(); jsonIterator = FileUtils.lineIterator(new File(jsonFile), "UTF-8"); } catch (IOException e) { throw new ResourceInitializationException(e); } }
From source file:fr.ericlab.mabed.structure.Corpus.java
public void prepareCorpus() { System.out.println(Util.getDate() + " Preparing corpus..."); String[] fileArray = new File("input/").list(); nbTimeSlices = 0;//from w w w . j av a2 s .com NumberFormat formatter = new DecimalFormat("00000000"); ArrayList<Integer> list = new ArrayList<>(); for (String filename : fileArray) { if (filename.endsWith(".text")) { try { list.add(formatter.parse(filename.substring(0, 8)).intValue()); } catch (ParseException ex) { Logger.getLogger(Corpus.class.getName()).log(Level.SEVERE, null, ex); } nbTimeSlices++; } } int a = Collections.min(list), b = Collections.max(list); LineIterator it = null; try { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S"); it = FileUtils.lineIterator(new File("input/" + formatter.format(a) + ".time"), "UTF-8"); if (it.hasNext()) { Date parsedDate = dateFormat.parse(it.nextLine()); startTimestamp = new java.sql.Timestamp(parsedDate.getTime()); } it = FileUtils.lineIterator(new File("input/" + formatter.format(b) + ".time"), "UTF-8"); String lastLine = ""; while (it.hasNext()) { lastLine = it.nextLine(); } Date parsedDate = dateFormat.parse(lastLine); endTimestamp = new java.sql.Timestamp(parsedDate.getTime()); } catch (IOException | ParseException ex) { Logger.getLogger(Corpus.class.getName()).log(Level.SEVERE, null, ex); } finally { LineIterator.closeQuietly(it); } System.out.print(" - Computing word frequencies"); GlobalIndexer indexer = new GlobalIndexer(configuration.numberOfThreads, false); try { indexer.index("input/", configuration.stopwords); } catch (InterruptedException | IOException ex) { Logger.getLogger(Corpus.class.getName()).log(Level.SEVERE, null, ex); } indexer = new GlobalIndexer(configuration.numberOfThreads, true); try { indexer.index("input/", configuration.stopwords); } catch (InterruptedException | IOException ex) { Logger.getLogger(Corpus.class.getName()).log(Level.SEVERE, null, ex); } System.out.println(", 100% done."); }