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:ke.co.tawi.babblesms.server.utils.export.topups.AllTopupsExportUtil.java
/** * Creates a Microsoft Excel Workbook containing Topup activity provided in * a CSV text file. The format of the created file will be Office Open XML * (OOXML).//from ww w . ja v a2s .com * <p> * It expects the CSV to have the following columns from left to right:<br * /> * topup.uuid, topup.msisdn, topup.amount, network.name, topupStatus.status, * topup.topupTime * <p> * This method has been created to allow for large Excel files to be created * without overwhelming memory. * * * @param topupCSVFile a valid CSV text file. It should contain the full * path and name of the file e.g. "/tmp/export/topups.csv" * @param delimiter the delimiter used in the CSV file * @param excelFile the Microsoft Excel file to be created. It should * contain the full path and name of the file e.g. "/tmp/export/topups.xlsx" * @return whether the creation of the Excel file was successful or not */ public static boolean createExcelExport(final String topupCSVFile, final String delimiter, final String excelFile) { boolean success = true; int rowCount = 0; // To keep track of the row that we are on Row row; Map<String, CellStyle> styles; SXSSFWorkbook wb = new SXSSFWorkbook(5000); // keep 5000 rows in memory, exceeding rows will be flushed to disk // Each line of the file is approximated to be 200 bytes in size, // therefore 5000 lines are approximately 1 MB in memory // wb.setCompressTempFiles(true); // temporary files will be gzipped on disk Sheet sheet = wb.createSheet("Airtime Topup"); styles = createStyles(wb); PrintSetup printSetupTopup = sheet.getPrintSetup(); printSetupTopup.setLandscape(true); sheet.setFitToPage(true); // Set up the heading to be seen in the Excel sheet row = sheet.createRow(rowCount); Cell titleCell; row.setHeightInPoints(45); titleCell = row.createCell(0); titleCell.setCellValue("Airtime Topups"); sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$L$1")); titleCell.setCellStyle(styles.get("title")); rowCount++; row = sheet.createRow(rowCount); row.setHeightInPoints(12.75f); for (int i = 0; i < TOPUP_TITLES.length; i++) { Cell cell = row.createCell(i); cell.setCellValue(TOPUP_TITLES[i]); cell.setCellStyle(styles.get("header")); } rowCount++; FileUtils.deleteQuietly(new File(excelFile)); FileOutputStream out; try { FileUtils.touch(new File(excelFile)); // Read the CSV file and populate the Excel sheet with it LineIterator lineIter = FileUtils.lineIterator(new File(topupCSVFile)); String line; String[] lineTokens; int size; while (lineIter.hasNext()) { row = sheet.createRow(rowCount); line = lineIter.next(); lineTokens = StringUtils.split(line, delimiter); size = lineTokens.length; for (int cellnum = 0; cellnum < size; cellnum++) { Cell cell = row.createCell(cellnum); cell.setCellValue(lineTokens[cellnum]); } rowCount++; } out = new FileOutputStream(excelFile); wb.write(out); out.close(); } catch (FileNotFoundException e) { logger.error("FileNotFoundException while trying to create Excel file '" + excelFile + "' from CSV file '" + topupCSVFile + "'."); logger.error(ExceptionUtils.getStackTrace(e)); success = false; } catch (IOException e) { logger.error("IOException while trying to create Excel file '" + excelFile + "' from CSV file '" + topupCSVFile + "'."); logger.error(ExceptionUtils.getStackTrace(e)); success = false; } wb.dispose(); // dispose of temporary files backup of this workbook on disk return success; }
From source file:com.abiquo.vsm.migration.Migrator.java
public void migrateNonPersistedModelFromFile(final File file) throws IOException { LineIterator iterator = FileUtils.lineIterator(file); RedisDao dao = RedisDaoFactory.getInstance(); try {/*from www . j a va 2 s . c om*/ while (iterator.hasNext()) { String line = iterator.nextLine(); insertMachineFromCSVLine(dao, line); } } finally { LineIterator.closeQuietly(iterator); } }
From source file:com.aionemu.gameserver.dataholders.DataLoader.java
/** * This method is loading data from particular .txt file. * * @param file a file which the data is loaded from.<br> * The method is loading the file row by row, omitting those started with * "#" sign.<br>/* w w w .jav a 2 s .co m*/ * Every read row is then forwarded to {@link #parse(String)} method, which * should be overriden in subclcass. */ private void loadFile(File file) { LineIterator it = null; try { it = FileUtils.lineIterator(file); while (it.hasNext()) { String line = it.nextLine(); if (line.isEmpty() || line.startsWith("#")) { continue; } parse(line); } } catch (IOException e) { log.error("Error while loading " + getClass().getSimpleName() + ", file: " + file.getPath(), e); } finally { LineIterator.closeQuietly(it); } }
From source file:com.opengamma.bbg.replay.BloombergRefDataCollector.java
private Set<String> loadFields() { Set<String> fields = Sets.newHashSet(); LineIterator it;/*from w ww .j av a 2 s. c o m*/ try { it = FileUtils.lineIterator(_fieldsFile); } catch (IOException ex) { throw new OpenGammaRuntimeException("IOException when reading " + _fieldsFile, ex); } try { while (it.hasNext()) { String line = it.nextLine(); if (StringUtils.isBlank(line) || line.charAt(0) == '#') { continue; } fields.add(line); } } finally { LineIterator.closeQuietly(it); } return fields; }
From source file:com.sangupta.murmur.MurmurEnglishTest.java
/** * The main core logic for all testing./*from w w w . jav a 2 s. co m*/ * * @param outputFileName * @param function * @throws IOException */ private void testHashes(String outputFileName, StringHashFunction function) throws IOException { LineIterator iterator = FileUtils.lineIterator(new File(BASE_PATH + "/english-wordlist.txt")); LineIterator results = FileUtils.lineIterator(new File(BASE_PATH + "/" + outputFileName)); int matched = 0; int total = 0; while (iterator.hasNext()) { String line = iterator.next(); byte[] bytes = line.getBytes(); String computed = function.getHash(bytes); String actual = results.next(); if (actual.contains(",")) { // result has multiple values String[] act = actual.split(","); String[] com = computed.split(","); if (act.length == com.length) { boolean allMatch = true; for (int index = 0; index < act.length; index++) { allMatch = allMatch & bigMatch(act[index], com[index]); } if (allMatch) { matched++; } } } else { // result has only a single value if (actual.equals(computed)) { matched++; } else { if (bigMatch(actual, computed)) { matched++; } } } total++; } Assert.assertEquals("Total number of hashes did not match", total, matched); }
From source file:com.bluexml.side.util.dependencies.MavenUtil.java
@SuppressWarnings("unchecked") private MavenExecutionResult doMavenGoalUsingMavenCli(File baseDir, List<String> goals, Map<String, String> parameters, List<String> profiles, Boolean offline) throws Exception { // save the current classloader ... maven play with thread classloader Grrr ClassLoader cl = Thread.currentThread().getContextClassLoader(); // Instantiate MavenClient MavenCli mci = new MavenCli(); String workingDirectory = baseDir.getAbsolutePath(); // build arguments list List<String> argsL = new ArrayList<String>(); argsL.addAll(goals);// ww w .j av a 2 s . co m // Additional parameters // disable interactive mode argsL.add("-B"); //$NON-NLS-1$ // display stacktrace if error occur argsL.add("-e"); //$NON-NLS-1$ // argsL.add("-X"); //$NON-NLS-1$ if (offline == null) { offline = false; } // offline mode activated if (offline) { argsL.add("-o"); //$NON-NLS-1$ } // active profile parameter if (profiles != null && profiles.size() > 0) { String profileParam = ""; //$NON-NLS-1$ Iterator<String> iterator = profiles.iterator(); while (iterator.hasNext()) { profileParam += iterator.next(); if (iterator.hasNext()) { profileParam += ","; //$NON-NLS-1$ } } argsL.add("-P " + profileParam); //$NON-NLS-1$ } // user Properties if (parameters != null) { for (Map.Entry<String, String> entry : parameters.entrySet()) { argsL.add("-D" + entry.getKey() + "=" + entry.getValue()); //$NON-NLS-1$ //$NON-NLS-2$ } } // define streams // TODO use PrintStreamLogger to implement maven logging and error detection File mvOutFile = new File(baseDir, "log.txt"); //$NON-NLS-1$ PrintStream stdout = new PrintStream(mvOutFile); File mvOutErrFile = new File(baseDir, "log-err.txt"); //$NON-NLS-1$ PrintStream stderr = new PrintStream(mvOutErrFile); stdout.println("MavenUtil execute maven request :"); //$NON-NLS-1$ String commandFromMavenExecutionArgs = getCommandFromMavenExecutionArgs(argsL); stdout.println("** args :" + commandFromMavenExecutionArgs); //$NON-NLS-1$ stdout.println("** working directory :" + workingDirectory); //$NON-NLS-1$ System.out.println("MavenUtil.doMavenGoalUsingMavenCli() dir :" + workingDirectory); System.out.println("MavenUtil.doMavenGoalUsingMavenCli() > " + commandFromMavenExecutionArgs); String[] args = argsL.toArray(new String[argsL.size()]); // execute maven request mci.doMain(args, workingDirectory, stdout, stderr); // build a MavenEcecutionResult DefaultMavenExecutionResult defaultMavenExecutionResult = new DefaultMavenExecutionResult(); // restore classloader Thread.currentThread().setContextClassLoader(cl); // search in output for errors Iterator<String> it = FileUtils.lineIterator(mvOutFile); List<String> errorLines = new ArrayList<String>(); String errors = ""; //$NON-NLS-1$ while (it.hasNext()) { String line = it.next(); if (line.startsWith("[ERROR]")) { //$NON-NLS-1$ errorLines.add(line); errors += line; errors += "\n"; //$NON-NLS-1$ } } if (errorLines.size() > 0) { defaultMavenExecutionResult.addException(new Exception(errors)); } // close output streams stdout.close(); stderr.close(); return defaultMavenExecutionResult; }
From source file:hu.bme.mit.sette.tools.spf.SpfParser.java
@Override protected void parseSnippet(Snippet snippet, SnippetInputsXml inputsXml) throws Exception { File outputFile = RunnerProjectUtils.getSnippetOutputFile(getRunnerProjectSettings(), snippet); File errorFile = RunnerProjectUtils.getSnippetErrorFile(getRunnerProjectSettings(), snippet); if (errorFile.exists()) { // TODO make this section simple and clear List<String> lines = FileUtils.readLines(errorFile); String firstLine = lines.get(0); if (firstLine.startsWith("java.lang.RuntimeException: ## Error: Operation not supported!")) { inputsXml.setResultType(ResultType.NA); } else if (firstLine.startsWith("java.lang.NullPointerException")) { inputsXml.setResultType(ResultType.EX); } else if (firstLine .startsWith("java.lang.RuntimeException: ## Error: symbolic log10 not implemented")) { inputsXml.setResultType(ResultType.NA); } else if (firstLine.startsWith("***********Warning: everything false")) { // TODO enhance // now skip } else if (snippet.getMethod().toString().contains("_Constants") || snippet.getMethod().toString().contains(".always()")) { // TODO JPF/SPF compilation differences between javac and ecj: // https://groups.google.com/forum/#!topic/java-pathfinder/jhOkvLx-SKE // now just accept } else {//from w ww . ja v a 2 s . com // TODO error handling // this is debug (only if unhandled error) System.err.println("============================="); System.err.println(snippet.getMethod()); System.err.println("============================="); for (String line : lines) { System.err.println(line); } System.err.println("============================="); // TODO error handling throw new RuntimeException("PARSER PROBLEM, UNHANDLED ERROR"); } } if (inputsXml.getResultType() == null) { // TODO enhance inputsXml.setResultType(ResultType.S); if (snippet.getMethod().toString().contains("_Constants") || snippet.getMethod().toString().contains(".always()")) { // TODO JPF/SPF compilation differences between javac and ecj: // https://groups.google.com/forum/#!topic/java-pathfinder/jhOkvLx-SKE // now just accept // no inputs for constant tests, just call them once inputsXml.getGeneratedInputs().add(new InputElement()); } else { LineIterator lines = FileUtils.lineIterator(outputFile); // find input lines List<String> inputLines = new ArrayList<>(); boolean shouldCollect = false; while (lines.hasNext()) { String line = lines.next(); if (line.trim() .equals("====================================================== Method Summaries")) { shouldCollect = true; } else if (shouldCollect) { if (line.startsWith("======================================================")) { // start of next section shouldCollect = false; break; } else { if (!StringUtils.isBlank(line)) { inputLines.add(line.trim()); } } } } // close iterator lines.close(); // remove duplicates inputLines = new ArrayList<>(new LinkedHashSet<>(inputLines)); System.out.println(snippet.getMethod()); String firstLine = inputLines.get(0); assert (firstLine.startsWith("Inputs: ")); firstLine = firstLine.substring(7).trim(); String[] parameterStrings = StringUtils.split(firstLine, ','); ParameterType[] parameterTypes = new ParameterType[parameterStrings.length]; if (inputLines.size() == 2 && inputLines.get(1).startsWith("No path conditions for")) { InputElement input = new InputElement(); for (int i = 0; i < parameterStrings.length; i++) { // no path conditions, only considering the "default" // inputs Class<?> type = snippet.getMethod().getParameterTypes()[i]; parameterTypes[i] = getParameterType(type); input.getParameters() .add(new ParameterElement(parameterTypes[i], getDefaultParameterString(type))); } inputsXml.getGeneratedInputs().add(input); } else { // parse parameter types Class<?>[] paramsJavaClass = snippet.getMethod().getParameterTypes(); for (int i = 0; i < parameterStrings.length; i++) { String parameterString = parameterStrings[i]; Class<?> pjc = ClassUtils.primitiveToWrapper(paramsJavaClass[i]); if (parameterString.endsWith("SYMINT")) { if (pjc == Boolean.class) { parameterTypes[i] = ParameterType.BOOLEAN; } else if (pjc == Byte.class) { parameterTypes[i] = ParameterType.BYTE; } else if (pjc == Short.class) { parameterTypes[i] = ParameterType.SHORT; } else if (pjc == Integer.class) { parameterTypes[i] = ParameterType.INT; } else if (pjc == Long.class) { parameterTypes[i] = ParameterType.LONG; } else { // int for something else parameterTypes[i] = ParameterType.INT; } } else if (parameterString.endsWith("SYMREAL")) { if (pjc == Float.class) { parameterTypes[i] = ParameterType.FLOAT; } else if (pjc == Float.class) { parameterTypes[i] = ParameterType.DOUBLE; } else { // int for something else parameterTypes[i] = ParameterType.DOUBLE; } } else if (parameterString.endsWith("SYMSTRING")) { parameterTypes[i] = ParameterType.EXPRESSION; } else { // TODO error handling // int for something else System.err.println(parameterString); throw new RuntimeException("PARSER PROBLEM"); } } // example // inheritsAPIGuessTwoPrimitives(11,-2147483648(don't care)) // --> // "java.lang.IllegalArgumentException..." // inheritsAPIGuessTwoPrimitives(9,11) --> // "java.lang.IllegalArgumentException..." // inheritsAPIGuessTwoPrimitives(7,9) --> // "java.lang.RuntimeException: Out of range..." // inheritsAPIGuessTwoPrimitives(4,1) --> Return Value: 1 // inheritsAPIGuessTwoPrimitives(0,0) --> Return Value: 0 // inheritsAPIGuessTwoPrimitives(9,-88) --> // "java.lang.IllegalArgumentException..." // inheritsAPIGuessTwoPrimitives(-88,-2147483648(don't // care)) // --> "java.lang.IllegalArgumentException..." String ps = String.format("^%s\\((.*)\\)\\s+-->\\s+(.*)$", snippet.getMethod().getName()); // ps = String.format("^%s(.*)\\s+-->\\s+(.*)$", // snippet.getMethod() // .getName()); ps = String.format("^(%s\\.)?%s(.*)\\s+-->\\s+(.*)$", snippet.getContainer().getJavaClass().getName(), snippet.getMethod().getName()); Pattern p = Pattern.compile(ps); // parse inputs int i = -1; for (String line : inputLines) { i++; if (i == 0) { // first line continue; } else if (StringUtils.isEmpty(line)) { continue; } Matcher m = p.matcher(line); if (m.matches()) { String paramsString = StringUtils.substring(m.group(2).trim(), 1, -1); String resultString = m.group(3).trim(); paramsString = StringUtils.replace(paramsString, "(don't care)", ""); String[] paramsStrings = StringUtils.split(paramsString, ','); InputElement input = new InputElement(); // if index error -> lesser inputs than parameters for (int j = 0; j < parameterTypes.length; j++) { if (parameterTypes[j] == ParameterType.BOOLEAN && paramsStrings[j].contains("-2147483648")) { // don't care -> 0 paramsStrings[j] = "false"; } ParameterElement pe = new ParameterElement(parameterTypes[j], paramsStrings[j].trim()); try { // just check the type format pe.validate(); } catch (Exception e) { // TODO error handling System.out.println(parameterTypes[j]); System.out.println(paramsStrings[j]); System.out.println(pe.getType()); System.out.println(pe.getValue()); e.printStackTrace(); System.err.println("============================="); System.err.println(snippet.getMethod()); System.err.println("============================="); for (String lll : inputLines) { System.err.println(lll); } System.err.println("============================="); System.exit(-1); } input.getParameters().add(pe); } if (resultString.startsWith("Return Value:")) { // has retval, nothing to do } else { // exception; example (" is present inside the // string!!!): // "java.lang.ArithmeticException: div by 0..." // "java.lang.IndexOutOfBoundsException: Index: 1, Size: 5..." int pos = resultString.indexOf(':'); if (pos < 0) { // not found :, search for ... pos = resultString.indexOf("..."); } String ex = resultString.substring(1, pos); input.setExpected(ex); // System.err.println(resultString); // System.err.println(ex); // // input.setExpected(expected); } inputsXml.getGeneratedInputs().add(input); } else { System.err.println("NO MATCH"); System.err.println(ps); System.err.println(line); throw new Exception("NO MATCH: " + line); } } } } inputsXml.validate(); } }
From source file:com.ikon.util.FormatUtil.java
/** * Parser log file/*from w ww .j a va2s . co m*/ */ public static Collection<LogMessage> parseLog(File flog, int begin, int end, String str) throws IOException { //log.debug("parseLog({}, {}, {}, {})", new Object[] { flog, begin, end, str }); ArrayList<LogMessage> al = new ArrayList<LogMessage>(); int i = 0; if (begin < 0 || end < 0) { int maxLines = 0; for (LineIterator lit = FileUtils.lineIterator(flog); lit.hasNext();) { lit.nextLine(); maxLines++; } if (begin < 0) { begin += maxLines; } if (end < 0) { end += maxLines + 1; } } for (LineIterator lit = FileUtils.lineIterator(flog); lit.hasNext();) { String line = lit.nextLine(); int idx = line.indexOf(str); i++; if ((str == null || idx > -1) && i >= begin && i <= end) { if (idx > -1) { StringBuilder sb = new StringBuilder(); sb.append(line.substring(0, idx)); sb.append("<span class=\"highlight\">"); sb.append(line.substring(idx, idx + str.length())); sb.append("</span>"); sb.append(line.substring(idx + str.length())); line = sb.toString(); } LogMessage lm = new LogMessage(); lm.setLine(i); lm.setMessage(line); al.add(lm); } } //log.debug("parseLog: {}", al); return al; }
From source file:com.googlecode.jgenhtml.CoverageReport.java
/** * Parses a gcov tracefile./* w ww. j ava2 s.c om*/ * @param traceFile A gcov tracefile. * @param isDescFile true if this is a descriptions (.desc) file. * @param isBaseFile true if this is a baseline file. */ private void parseDatFile(final File traceFile, final boolean isDescFile, final boolean isBaseFile) throws IOException, ParserConfigurationException { //I used the info from here: http://manpages.ubuntu.com/manpages/precise/man1/geninfo.1.html File fileToProcess; if (traceFile.getName().endsWith(".gz")) { LOGGER.log(Level.FINE, "File {0} ends with .gz, going to gunzip it.", traceFile.getName()); fileToProcess = JGenHtmlUtils.gunzip(traceFile); } else { fileToProcess = traceFile; } LineIterator iterator = FileUtils.lineIterator(fileToProcess); try { TestCaseSourceFile testCaseSourceFile = null; String testCaseName = DEFAULT_TEST_NAME; while (iterator.hasNext()) { String line = iterator.nextLine(); int tokenIdx = line.indexOf("SF:"); if (tokenIdx >= 0 || (tokenIdx = line.indexOf("KF:")) >= 0) { String fullPath = line.substring(line.indexOf(tokenIdx) + 4); File sourceFile = new File(fullPath); fullPath = sourceFile.getCanonicalPath(); testCaseSourceFile = parsedFiles.get(fullPath); if (!isBaseFile && testCaseSourceFile == null) { testCaseSourceFile = new TestCaseSourceFile(testTitle, sourceFile.getName()); testCaseSourceFile.setSourceFile(sourceFile); parsedFiles.put(fullPath, testCaseSourceFile); } } else if (line.indexOf("end_of_record") >= 0) { if (testCaseSourceFile != null) { testCaseName = DEFAULT_TEST_NAME; testCaseSourceFile = null; } else { LOGGER.log(Level.FINE, "Unexpected end of record"); } } else if (testCaseSourceFile != null) { testCaseSourceFile.processLine(testCaseName, line, isBaseFile); } else { if (isDescFile) { descriptionsPage.addLine(line); } else if (line.startsWith("TN:")) { String[] data = JGenHtmlUtils.extractLineValues(line); testCaseName = data[0].trim(); if (testCaseName.length() > 0) { if (runTestNames == null) { runTestNames = new HashSet<String>(); } runTestNames.add(testCaseName); } } else { LOGGER.log(Level.FINE, "Unexpected line: {0}", line); } } } } finally { LineIterator.closeQuietly(iterator); } }
From source file:com.openkm.util.FormatUtil.java
/** * Parser log file// www .ja v a 2 s .co m */ public static Collection<LogMessage> parseLog(File flog, int begin, int end, String str) throws IOException { // log.debug("parseLog({}, {}, {}, {})", new Object[] { flog, begin, end, str }); ArrayList<LogMessage> al = new ArrayList<LogMessage>(); int i = 0; if (begin < 0 || end < 0) { int maxLines = 0; for (LineIterator lit = FileUtils.lineIterator(flog); lit.hasNext();) { lit.nextLine(); maxLines++; } if (begin < 0) { begin += maxLines; } if (end < 0) { end += maxLines + 1; } } for (LineIterator lit = FileUtils.lineIterator(flog); lit.hasNext();) { String line = lit.nextLine(); int idx = line.indexOf(str); i++; if ((str == null || idx > -1) && i >= begin && i <= end) { if (idx > -1) { StringBuilder sb = new StringBuilder(); sb.append(line.substring(0, idx)); sb.append("<span class=\"highlight\">"); sb.append(line.substring(idx, idx + str.length())); sb.append("</span>"); sb.append(line.substring(idx + str.length())); line = sb.toString(); } LogMessage lm = new LogMessage(); lm.setLine(i); lm.setMessage(line); al.add(lm); } } // log.debug("parseLog: {}", al); return al; }