List of usage examples for org.apache.commons.io LineIterator nextLine
public String nextLine()
Reader
. From source file:org.jcvi.ometa.engine.LoadingEngine.java
public void batchLoad() throws Exception { String userName = usage.getUsername(); String passWord = usage.getPassword(); String serverUrl = usage.getServerUrl(); String eventFileName = usage.getInputFilename(); String eventName = usage.getEventName(); String projectName = usage.getProjectName(); String outputPath = usage.getOutputLocation(); int batchSizeInt = 1; String batchSize = usage.getBatchSize(); if (batchSize != null && !batchSize.isEmpty()) { batchSizeInt = Integer.parseInt(batchSize); }//w ww .j a v a2 s . c o m File eventFile = new File(eventFileName); Long timeStamp = new Date().getTime(); File logFile = new File(outputPath + File.separator + "ometa.log"); FileWriter logWriter = new FileWriter(logFile, false); int successCount = 0; File processedFile = new File(outputPath + File.separator + "ometa_processed.csv"); FileWriter processedWriter = new FileWriter(processedFile, false); int failedCount = 0; File failedFile = new File(outputPath + File.separator + "ometa_failed.csv"); FileWriter failedWriter = new FileWriter(failedFile, false); // Must break this file up, and deposit it into a temporary output directory. String userBase = System.getProperty("user.home"); ScratchUtils.setScratchBaseLocation(userBase + "/" + Constants.SCRATCH_BASE_LOCATION); File scratchLoc = ScratchUtils.getScratchLocation(timeStamp, "LoadingEngine__" + eventFile.getName()); int processedLineCount = 0; try { BeanWriter writer = new BeanWriter(serverUrl, userName, passWord); LineIterator lineIterator = FileUtils.lineIterator(eventFile); int lineCount = 0; String headerLine = null; while (lineIterator.hasNext()) { ++lineCount; String currLine = lineIterator.nextLine(); if (lineCount == 1) { headerLine = currLine; processedWriter.write(currLine + "\n"); failedWriter.write(currLine + "\n"); continue; } else if (lineCount == 2 && (currLine.startsWith("#") || currLine.startsWith("\"#"))) { //skip comment line continue; } else { File singleEventFile = new File(scratchLoc.getAbsoluteFile() + File.separator + "temp.csv"); List<String> lines = new ArrayList<String>(2); lines.add(headerLine); lines.add(currLine); FileUtils.writeLines(singleEventFile, lines); try { String eventTarget = writer.writeEvent(singleEventFile, eventName, projectName, true); logWriter.write(String.format("[%d] loaded event for %s\n", lineCount, eventTarget)); processedWriter.write(currLine + "\n"); successCount++; } catch (Exception ex) { failedWriter.write(currLine + "\n"); logWriter.write(String.format("[%d] failed :\n", lineCount)); logWriter.write(ex.getMessage() + "\n"); failedCount++; } processedLineCount++; } } } catch (IOException ioe) { System.err.println("IOException: " + ioe.getMessage()); } catch (Exception ex) { String exceptionString = ex.getCause() == null ? ex.getMessage() : ex.getCause().getMessage(); logWriter.write(exceptionString); throw ex; } finally { if (scratchLoc != null && scratchLoc.exists()) { File cleanupDir = scratchLoc; String parentDirName = cleanupDir.getParentFile().getName(); try { Long.parseLong(parentDirName); cleanupDir = cleanupDir.getParentFile(); } catch (NumberFormatException nf) { } FileUtils.forceDelete(cleanupDir); } logWriter.close(); processedWriter.close(); failedWriter.close(); System.out.printf("total: %d, processed: %d, failed: %d\n", processedLineCount, successCount, failedCount); System.out.println("log file: " + logFile.getAbsolutePath()); } }
From source file:org.jdev.emg.sonar.cci.CCIXmlMetricsDecorator.java
@Override public void decorate(Resource resource, DecoratorContext context) { if (!Qualifiers.isFile(resource)) { return;/*from w w w .j a va 2s .c om*/ } ProjectFileSystem fileSystem = context.getProject().getFileSystem(); File file = lookup(resource, fileSystem); try { if (readFirstByte(file) != '<') { return; } } catch (IOException e) { throw new SonarException(e); } int numCommentLines; CCICountCommentParser commentCounter = new CCICountCommentParser(); 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.jenkinsci.plugins.autozoil.AutozoilSource.java
private void copyLineWithHighlightedContext(final StringBuilder output, final LineIterator lineIterator, String color, List<AutozoilFile> autozoilFiles) { String line = lineIterator.nextLine(); for (AutozoilFile autozoilFile : autozoilFiles) { String substringToSearch = autozoilFile.getContext(); if (substringToSearch == null) continue; // It should escape all HTML-specific characters substringToSearch = substringToSearch.replaceAll(" ", " "); //FIXME: substring is not found if it contains Polish diacritical signs int pos = line.indexOf(substringToSearch); if (pos == -1) continue; line = line.substring(0, pos) + "<span style=\"color:#fff; padding:3px 3px 4px; background-color:" + color + "\">" + line.substring(pos, pos + substringToSearch.length()) + "</span>" + line.substring(pos + substringToSearch.length()); }//from www . j av a 2 s . c om output.append(line); output.append("\n"); }
From source file:org.jibenakka.sample.mapreduce.wordcount.worker.Mapper.java
/** * * @param work//from ww w .java 2 s . c o m */ protected void extractFileMapResult(InitialMapWork work) { LinkedList<Future<Map<String, Integer>>> futureResults = new LinkedList<Future<Map<String, Integer>>>(); LineIterator lineIterator = null; try { File fileToCount = work.getFileToCount(); lineIterator = FileUtils.lineIterator(fileToCount); while (lineIterator.hasNext()) { // All work including special character handling done at worker // level String line = lineIterator.nextLine(); // key is just the file name - initial mapping is easy // hard part comes with partitioning and reduction // we assume that we have unique file names in the dir MapWork newWork = new MapWork(fileToCount.getName(), line); Future<Map<String, Integer>> future = this.workRouter.sendRequestReplyFuture(newWork, 30000, getContext()); future.await(); futureResults.add(future); } // FinalMapResult result = new FinalMapResult( // (LinkedList<Future<Map<String, Integer>>>) futureResults); getContext().channel().sendOneWay(futureResults); } catch (IOException e) { e.printStackTrace(); } finally { if (lineIterator != null) { lineIterator.close(); } } }
From source file:org.kalypso.model.wspm.tuhh.schema.simulation.LengthSectionParser.java
private IStatus processLSFile(final String header, final String footer, final LogHelper log) throws FileNotFoundException, IOException { final Collection<IStatus> result = new ArrayList<>(); final WspmWaterBody waterBody = m_calculation.getReach().getWaterBody(); final TuhhStationComparator stationComparator = new TuhhStationComparator(waterBody.isDirectionUpstreams()); LineIterator lineIterator = null; ResultLengthSection lsProc = null;/* w w w . j av a 2 s. co m*/ try { lineIterator = IOUtils.lineIterator(new FileInputStream(m_lsFile), IWspmTuhhConstants.WSPMTUHH_CODEPAGE); BigDecimal firstStation = null; // station of previous line while (lineIterator.hasNext()) { if (log.checkCanceled()) return Status.CANCEL_STATUS; final String nextLine = lineIterator.nextLine(); /* Introduce space around 'NaN' and '***' values to make it parseable */ if (nextLine.contains("NaN")) //$NON-NLS-1$ log.log(false, Messages.getString("LengthSectionParser.0")); //$NON-NLS-1$ // TODO: handle NaN-values to keep information alive (unfortunally BigDecimal throws a NumberFormatException) final String cleanLine1 = nextLine.replaceAll("-NaN", " null "); //$NON-NLS-1$ //$NON-NLS-2$ final String cleanLine2 = cleanLine1.replaceAll("NaN", " null "); //$NON-NLS-1$ //$NON-NLS-2$ final String cleanLine3 = cleanLine2.replaceAll("-999.999", " null "); //$NON-NLS-1$ //$NON-NLS-2$ final BigDecimal station = NumberUtils.parseQuietDecimal(cleanLine3, 0, 11, IWspmTuhhConstants.STATION_SCALE); final BigDecimal runoff = NumberUtils.parseQuietDecimal(cleanLine3, 17, 27, 3); /* Any lines where station or runoff cannot be parsed are filtered out */ if (Objects.isNull(station, runoff)) continue; /* A new section begins, if the new station is lower than the next station */ final boolean sectionEnd = firstStation == null || stationComparator.compare(firstStation, station) == 0; if (sectionEnd) { if (lsProc != null) { final IStatus processorResult = closeProcessor(lsProc, footer); if (!processorResult.isOK()) result.add(processorResult); } log.log(false, Messages.getString( "org.kalypso.model.wspm.tuhh.schema.simulation.LengthSectionParser.2"), runoff); //$NON-NLS-1$ lsProc = new ResultLengthSection(runoff, m_outputDir, m_calculation, m_epsThinning, m_ovwMapURL); lsProc.addLine(header); lsProc.setTitlePattern(m_titlePattern); lsProc.setLsFilePattern(m_lsFilePattern); } /* clean line */ lsProc.addLine(cleanLine3); if (firstStation == null) firstStation = station; } if (lsProc != null) { final IStatus processorResult = closeProcessor(lsProc, footer); if (!processorResult.isOK()) result.add(processorResult); } } finally { LineIterator.closeQuietly(lineIterator); } if (result.isEmpty()) return Status.OK_STATUS; final IStatus[] children = result.toArray(new IStatus[result.size()]); final String msg = String.format(Messages.getString("LengthSectionParser.1")); //$NON-NLS-1$ return new MultiStatus(KalypsoModelWspmTuhhSchemaPlugin.getID(), 0, children, msg, null); }
From source file:org.kalypso.wspwin.core.CalculationBean.java
public static CalculationBean[] readBerFile(final File berFile) throws ParseException, IOException { // if a zustand has no calculations, no .ber file is present. if (!berFile.exists()) return new CalculationBean[0]; final List<CalculationBean> beans = new ArrayList<>(10); LineIterator lineIt = null; try {/*from w w w .j a va 2 s .c om*/ int count = 0; lineIt = FileUtils.lineIterator(berFile, "CP850"); //$NON-NLS-1$ // ignore first line, we just read all lines lineIt.nextLine(); count++; while (lineIt.hasNext()) { final String line = lineIt.nextLine(); count++; if (line.length() < 60) throw new ParseException(Messages.getString("org.kalypso.wspwin.core.CalculationBean.0") + line, //$NON-NLS-1$ count); final String name = line.substring(0, 60).trim(); final StringTokenizer tokenizer = new StringTokenizer(line.substring(60)); if (tokenizer.countTokens() != 3) throw new ParseException(Messages.getString("org.kalypso.wspwin.core.CalculationBean.1") + line, //$NON-NLS-1$ count); final BigDecimal fromStation = new BigDecimal(tokenizer.nextToken()); final BigDecimal toStation = new BigDecimal(tokenizer.nextToken()); final String fileName = tokenizer.nextToken(); beans.add(new CalculationBean(name, fileName, fromStation, toStation)); } return beans.toArray(new CalculationBean[beans.size()]); } finally { LineIterator.closeQuietly(lineIt); } }
From source file:org.kalypso.wspwin.core.LocalEnergyLossBean.java
/** * Reads a psi file (Energieverluste/Verlustbeiwerte/local energy losses) *///from w w w. ja va 2 s . c o m public static LocalEnergyLossBean[] read(final File lelFile) throws ParseException, IOException { final List<LocalEnergyLossBean> beans = new ArrayList<>(0); LineIterator lineIt = null; try { if (lelFile.exists()) { int count = 0; for (lineIt = FileUtils.lineIterator(lelFile, null); lineIt.hasNext();) { final String nextLine = lineIt.nextLine(); count++; final StringTokenizer tokenizer = new StringTokenizer(nextLine); if (tokenizer.countTokens() % 2 != 0) throw new ParseException( Messages.getString("org.kalypso.wspwin.core.LocalEnergyLossBean.1") + nextLine, //$NON-NLS-1$ count); final int countKinds = tokenizer.countTokens() / 2 - 1; final String key = tokenizer.nextToken(); if (!STATION.equalsIgnoreCase(key)) throw new ParseException(Messages.getString("org.kalypso.wspwin.core.LocalEnergyLossBean.2") //$NON-NLS-1$ + STATION + "': " + nextLine, count); //$NON-NLS-1$ final BigDecimal station = new BigDecimal(tokenizer.nextToken()); // read pairs: kind -> value final Collection<Pair<String, BigDecimal>> entries = new ArrayList<>(); for (int i = 0; i < countKinds; i++) { final String kind = tokenizer.nextToken(); final BigDecimal value = new BigDecimal(tokenizer.nextToken()); entries.add(Pair.of(kind, value)); } final LocalEnergyLossBean lossBean = new LocalEnergyLossBean(station, entries.toArray(new Pair[entries.size()])); beans.add(lossBean); } } return beans.toArray(new LocalEnergyLossBean[beans.size()]); } finally { LineIterator.closeQuietly(lineIt); } }
From source file:org.kalypso.wspwin.core.RunOffEventBean.java
/** Reads a qwt or wsf file */ public static RunOffEventBean[] read(final File qwtFile) throws ParseException, IOException { // the qwt and wsf files may not exist; return empty list of beans if (!qwtFile.exists()) return new RunOffEventBean[] {}; final List<RunOffEventBean> beans = new ArrayList<>(10); LineIterator lineIt = null; try {/*from w ww. j av a2s .c o m*/ int count = 0; for (lineIt = FileUtils.lineIterator(qwtFile, null); lineIt.hasNext();) { final String nextLine = lineIt.nextLine().trim(); count++; if (nextLine.isEmpty()) continue; final StringTokenizer tokenizer = new StringTokenizer(nextLine); if (tokenizer.countTokens() != 2) throw new ParseException( Messages.getString("org.kalypso.wspwin.core.RunOffEventBean.0") + nextLine, count); //$NON-NLS-1$ final String eventName = tokenizer.nextToken(); final RunOffEventBean bean = new RunOffEventBean(eventName); final int eventLength = Integer.parseInt(tokenizer.nextToken()); // read block: station -> value for (int i = 0; i < eventLength; i++) { if (!lineIt.hasNext()) throw new ParseException( Messages.getString("org.kalypso.wspwin.core.RunOffEventBean.1") + eventName, count); //$NON-NLS-1$ final String line = lineIt.nextLine(); count++; final StringTokenizer tz = new StringTokenizer(line); if (tz.countTokens() != 2) throw new ParseException( Messages.getString("org.kalypso.wspwin.core.RunOffEventBean.2") + nextLine, count); //$NON-NLS-1$ final double station = Double.parseDouble(tz.nextToken()); final double value = Double.parseDouble(tz.nextToken()); bean.addEntry(BigDecimal.valueOf(station), BigDecimal.valueOf(value)); } beans.add(bean); } return beans.toArray(new RunOffEventBean[beans.size()]); } finally { LineIterator.closeQuietly(lineIt); } }
From source file:org.klab.com.etl.Movies.java
public void readFile(String file, BufferedWriter bw) { int _n_ = 0;/*from w ww.j a v a 2 s . co m*/ int _r_ = 0; LineIterator it = null; String FILE_DATE = movieFileDate(file); try { it = FileUtils.lineIterator(new File(file), "UTF-8"); while (it.hasNext()) { String line = it.nextLine(); int amountOfTabsLine = StringUtils.countMatches(line, SEPARATOR); //This will decide the structure String fixedLine = fixLine(line); // do something with line // System.out.println("N:\t"+amountOfTabsLine+ "\t"+line); if (!fixedLine.isEmpty() && _n_ > 0) { String output = _n_ + "\t" + FILE_DATE + "\t" + amountOfTabsLine + "\t" + fixedLine; bw.write(output); bw.newLine(); _r_++; } _n_++; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { System.out.println("io > [" + _n_ + "][" + _r_ + "] > out"); LineIterator.closeQuietly(it); } }
From source file:org.klab.com.etl.Movies.java
public void readinHeader(File file) { LineIterator it = null; try {//from www . j av a 2s. c o m it = FileUtils.lineIterator(file, "UTF-8"); if (it.hasNext()) { String line = it.nextLine(); getHeaderStructure(line); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { LineIterator.closeQuietly(it); } }