List of usage examples for org.apache.commons.io FileUtils lineIterator
public static LineIterator lineIterator(File file) throws IOException
File
using the default encoding for the VM. From source file:org.adf.emg.sonar.ojaudit.XmlMetricsDecorator.java
@Override public void decorate(Resource resource, DecoratorContext context) { if (!Qualifiers.isFile(resource)) { return;//from w ww .j a v a 2s . co m } ProjectFileSystem fileSystem = context.getProject().getFileSystem(); File file = lookup(resource, fileSystem); try { if (readFirstByte(file) != '<') { return; } } catch (IOException e) { throw new SonarException(e); } int numCommentLines; CountCommentParser commentCounter = new CountCommentParser(); try { numCommentLines = commentCounter.countLinesOfComment(FileUtils.openInputStream(file)); if (numCommentLines == -1) { return; } } catch (IOException e) { throw new SonarException(e); } LineIterator iterator = null; int numLines = 0; int numBlankLines = 0; try { Charset charset = fileSystem.getSourceCharset(); iterator = charset == null ? FileUtils.lineIterator(file) : FileUtils.lineIterator(file, charset.name()); while (iterator.hasNext()) { String line = iterator.nextLine(); numLines++; if (line.trim().isEmpty()) { numBlankLines++; } } } catch (IOException e) { LOG.warn("error reading " + file + " to collect metrics", e); } finally { LineIterator.closeQuietly(iterator); } context.saveMeasure(CoreMetrics.LINES, (double) numLines); // Lines context.saveMeasure(CoreMetrics.COMMENT_LINES, (double) numCommentLines); // Non Commenting Lines of Code context.saveMeasure(CoreMetrics.NCLOC, (double) numLines - numBlankLines - numCommentLines); // Comment Lines }
From source file:org.asqatasun.rules.doc.utils.exportaw22torgaa3ruledesign.ExtractCsvAndCopy.java
public Iterable<CSVRecord> getCsv() throws IOException { // we parse the csv file to extract the first line and get the headers LineIterator lineIterator;//from w ww .j a v a 2 s .c o m lineIterator = FileUtils.lineIterator(DATA_FILE); csvHeaders = lineIterator.next().split(String.valueOf(DELIMITER)); StringBuilder strb = new StringBuilder(); while (lineIterator.hasNext()) { strb.append(lineIterator.next()); strb.append("\n"); } Reader in; try { in = new StringReader(strb.toString()); CSVFormat csvf = CSVFormat.newFormat(DELIMITER).withHeader(csvHeaders); return csvf.parse(in); } catch (FileNotFoundException ex) { return null; } catch (IOException ex) { return null; } }
From source file:org.broadinstitute.gatk.utils.io.IOUtils.java
/** * Returns a file throwing a UserException if the file cannot be read. * @param file File/*from w w w .ja v a 2s . c om*/ * @return LineIterator */ public static LineIterator lineIterator(File file) { try { return FileUtils.lineIterator(file); } catch (IOException e) { throw new UserException.CouldNotReadInputFile(file, e); } }
From source file:org.cleverbus.admin.services.log.LogEventParsingIterator.java
/** * Returns a line iterator to process next/current file, opening the next file, if necessary. * * @return line iterator for the current/next file; or null if no files left */// w w w . jav a2s .c o m private LineIterator getLineIterator() throws IOException { if (lineIterator != null && lineIterator.hasNext()) { return lineIterator; } // discard current line iterator LineIterator.closeQuietly(lineIterator); lineIterator = null; if (fileEventsFound == 0) { Log.debug("No events in the last file, closing prematurely"); close(); // optimize: last file had no events, no point continuing } else if (!logFiles.isEmpty()) { File file = logFiles.poll(); Log.debug("Opening {}", file); lineIterator = FileUtils.lineIterator(file); fileEventsFound = 0; // restart per-file counter } return lineIterator; }
From source file:org.deeplearning4j.models.word2vec.iterator.Word2VecDataFetcher.java
@Override public DataSet next() { //pop from cache when possible, or when there's nothing left if (cache.size() >= batch || !files.hasNext()) return fromCache(); File f = files.next();//from www .java 2 s . com try { LineIterator lines = FileUtils.lineIterator(f); INDArray outcomes = null; INDArray input = null; while (lines.hasNext()) { List<Window> windows = Windows.windows(lines.nextLine()); if (windows.isEmpty() && lines.hasNext()) continue; if (windows.size() < batch) { input = Nd4j.create(windows.size(), vec.lookupTable().layerSize() * vec.getWindow()); outcomes = Nd4j.create(batch, labels.size()); for (int i = 0; i < windows.size(); i++) { input.putRow(i, WindowConverter.asExampleMatrix(cache.get(i), vec)); int idx = labels.indexOf(windows.get(i).getLabel()); if (idx < 0) idx = 0; INDArray outcomeRow = FeatureUtil.toOutcomeVector(idx, labels.size()); outcomes.putRow(i, outcomeRow); } return new DataSet(input, outcomes); } else { input = Nd4j.create(batch, vec.lookupTable().layerSize() * vec.getWindow()); outcomes = Nd4j.create(batch, labels.size()); for (int i = 0; i < batch; i++) { input.putRow(i, WindowConverter.asExampleMatrix(cache.get(i), vec)); int idx = labels.indexOf(windows.get(i).getLabel()); if (idx < 0) idx = 0; INDArray outcomeRow = FeatureUtil.toOutcomeVector(idx, labels.size()); outcomes.putRow(i, outcomeRow); } //add left over to cache; need to ensure that only batch rows are returned /* * Note that I'm aware of possible concerns for sentence sequencing. * This is a hack right now in place of something * that will be way more elegant in the future. */ if (windows.size() > batch) { List<Window> leftOvers = windows.subList(batch, windows.size()); cache.addAll(leftOvers); } return new DataSet(input, outcomes); } } } catch (IOException e) { throw new RuntimeException(e); } return null; }
From source file:org.deeplearning4j.text.documentiterator.FileDocumentIterator.java
public FileDocumentIterator(File path) { if (path.isFile()) { iter = Arrays.asList(path).iterator(); try {//w ww . j a va 2 s .c o m lineIterator = FileUtils.lineIterator(path); } catch (IOException e) { throw new RuntimeException(e); } this.rootDir = path; } else { iter = FileUtils.iterateFiles(path, null, true); try { lineIterator = FileUtils.lineIterator(iter.next()); } catch (IOException e) { throw new RuntimeException(e); } this.rootDir = path; } }
From source file:org.deeplearning4j.text.documentiterator.FileDocumentIterator.java
@Override public synchronized InputStream nextDocument() { try {/* www. j a v a 2s .c om*/ if (lineIterator != null && !lineIterator.hasNext() && iter.hasNext()) { File next = iter.next(); lineIterator.close(); lineIterator = FileUtils.lineIterator(next); while (!lineIterator.hasNext()) { lineIterator.close(); lineIterator = FileUtils.lineIterator(next); } } if (lineIterator != null && lineIterator.hasNext()) { return new BufferedInputStream(IOUtils.toInputStream(lineIterator.nextLine())); } } catch (Exception e) { log.warn("Error reading input stream...this is just a warning..Going to return", e); return null; } return null; }
From source file:org.deeplearning4j.text.sentenceiterator.FileSentenceIterator.java
private void nextLineIter() { if (fileIterator.hasNext()) { try {// w w w .ja v a 2 s.c o m File next = fileIterator.next(); currentFile = next; if (next.getAbsolutePath().endsWith(".gz")) { if (currLineIterator != null) currLineIterator.close(); currLineIterator = IOUtils.lineIterator( new BufferedInputStream(new GZIPInputStream(new FileInputStream(next))), "UTF-8"); } else { if (currLineIterator != null) { currLineIterator.close(); } currLineIterator = FileUtils.lineIterator(next); } } catch (IOException e) { throw new RuntimeException(e); } } }
From source file:org.eclipse.orion.internal.server.search.FileGrepper.java
/** * Searches the contents of a file//w w w .ja va 2 s.c om * @param file The file to search * @return returns whether the search was successful * @throws IOException thrown if there is an error reading the file */ private boolean searchFile(File file) { LineIterator lineIterator = null; try { lineIterator = FileUtils.lineIterator(file); } catch (IOException e) { logger.error("FileGrepper.searchFile: " + e.getLocalizedMessage()); return false; } try { while (lineIterator.hasNext()) { String line = lineIterator.nextLine(); if (line.contains("\0")) { // file contains binary content return false; } matcher.reset(line); if (matcher.find()) { return true; } } } finally { if (lineIterator != null) lineIterator.close(); } return false; }
From source file:org.eclipse.orion.internal.server.search.grep.FileGrepper.java
/** * Searches the contents of a file/* w w w. j a v a 2s . c o m*/ * @param file The file to search * @return returns whether the search was successful * @throws IOException thrown if there is an error reading the file */ private boolean searchFile(File file) { LineIterator lineIterator = null; try { lineIterator = FileUtils.lineIterator(file); } catch (IOException e) { logger.error("FileGrepper.searchFile: " + e.getLocalizedMessage()); return false; } try { while (lineIterator.hasNext()) { String line = lineIterator.nextLine(); matcher.reset(line); if (matcher.find()) { return true; } } } finally { if (lineIterator != null) lineIterator.close(); } return false; }