List of usage examples for org.apache.commons.io LineIterator hasNext
public boolean hasNext()
Reader
has more lines. From source file:com.mewmew.fairy.v1.book.Xargs.java
public static void exec(Map<String, String> env, String cmd[], boolean redirectError, Output<String> output) throws IOException, InterruptedException { ProcessBuilder pb = new ProcessBuilder(cmd); if (env != null) { pb.environment().putAll(env);/* w w w.j av a 2s . c o m*/ } if (redirectError) { pb.redirectErrorStream(true); } final Process p = pb.start(); if (!redirectError) { new Thread(new Runnable() { public void run() { try { LineIterator err = new LineIterator(new InputStreamReader(p.getErrorStream())); while (err.hasNext()) { err.next(); } } finally { } } }).start(); } LineIterator out = new LineIterator(new InputStreamReader(p.getInputStream())); while (out.hasNext()) { output.output(out.nextLine()); } int code = p.waitFor(); if (code != 0) { throw new RuntimeException(String.format("return != 0, code = %d", code)); } }
From source file:edu.umn.msi.tropix.proteomics.test.VerifyUtils.java
public static void verifyMGF(final InputStream stream) { final LineIterator lineIterator = IOUtils.lineIterator(new InputStreamReader(stream)); assert lineIterator.hasNext(); String line = lineIterator.nextLine(); assert line.startsWith("COM="); assert lineIterator.hasNext(); line = lineIterator.nextLine();//from w w w.j av a 2 s . c o m assert line.startsWith("CHARGE="); while (true) { if (!lineIterator.hasNext()) { break; } line = lineIterator.nextLine(); assert line.equals("BEGIN IONS") : line; line = lineIterator.nextLine(); assert line.matches( "PEPMASS=(\\d*\\.?\\d*)( (\\d*\\.?\\d*))?") : "Does not appear to be a peptide mass line " + line; line = lineIterator.nextLine(); assert line.matches("CHARGE=[\\d, \\+]+") : line; line = lineIterator.nextLine(); assert line.startsWith("SCANS=") : line; line = lineIterator.nextLine(); assert line.startsWith("TITLE=") : line; while (true) { line = lineIterator.nextLine(); if (line.equals("END IONS")) { break; } else if (line .matches(RegexUtils.FLOATING_POINT_LITERAL + "\\s+" + RegexUtils.FLOATING_POINT_LITERAL)) { continue; } else { assert false : "MGF contains invalid line " + line; } } } }
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 !!!"); }// ww w .java 2 s. c om 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:mitm.common.postfix.PostfixMainConfigParser.java
private static List<String> normalize(Reader config) { List<String> lines = new LinkedList<String>(); LineIterator it = new LineIterator(config); String line = ""; try {/*from ww w. j av a2 s . co m*/ while (it.hasNext()) { String nextLine = it.nextLine(); Matcher matcher = CONTINUATION.matcher(nextLine); if (!nextLine.trim().startsWith("#") && matcher.matches()) { /* its a continuation line */ line = line + LF + nextLine; } else { matcher = NAME_VALUE.matcher(nextLine); if (matcher.matches()) { /* its a name/value line */ if (StringUtils.isNotEmpty(line)) { /* store the previous line because it is finished */ lines.add(line); } line = nextLine; } else { /* it's some other line like a comment */ if (StringUtils.isNotEmpty(line)) { /* store the previous line because it is finished */ lines.add(line); } lines.add(nextLine); line = ""; } } } if (StringUtils.isNotEmpty(line)) { lines.add(line); } } finally { LineIterator.closeQuietly(it); } return lines; }
From source file:it.geosolutions.tools.io.file.writer.Writer.java
/** * Open 'destination' file in append mode and append content of the * 'toAppend' file/*from w ww . ja va 2s. c o m*/ * * @param toAppend * @param destination * @throws IOException */ public static void appendFile(File toAppend, File destination) throws IOException { FileWriter fw = null; BufferedWriter bw = null; LineIterator it = null; try { fw = new FileWriter(destination, true); bw = new BufferedWriter(fw); it = FileUtils.lineIterator(toAppend); while (it.hasNext()) { bw.append(it.nextLine()); bw.newLine(); } bw.flush(); } finally { if (it != null) { it.close(); } if (bw != null) { IOUtils.closeQuietly(bw); } if (fw != null) { IOUtils.closeQuietly(fw); } } }
From source file:com.polytech4A.cuttingstock.core.resolution.util.context.ContextLoaderUtils.java
/** * Line Parser of global first infomrations. * * @param iterator iterator of the openfile. * @param regex regex of the line./*w ww . j a v a 2s . c o m*/ * @return value loaded. * @throws MalformedContextFileException if the Context file don't have the right structure. */ private static Double loadLine(LineIterator iterator, String regex) throws MalformedContextFileException { MalformedContextFileException mctx = new MalformedContextFileException(); if (iterator.hasNext()) { String line = iterator.nextLine(); if (line.matches(regex)) { return Double.parseDouble(line.split("=")[1]); } else { throw mctx; } } else { throw mctx; } }
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; try {/*w ww. j a v a2s .com*/ 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.redhat.rhn.common.util.FileUtils.java
/** * Reads and returns the last n lines from a given file as string. * @param pathToFile path to file//from www .j a va 2 s . c om * @param lines size of the tail * @return tail of file as string */ public static String getTailOfFile(String pathToFile, Integer lines) { InputStream fileStream = null; CircularFifoBuffer buffer = new CircularFifoBuffer(lines); try { fileStream = new FileInputStream(pathToFile); LineIterator it = org.apache.commons.io.IOUtils.lineIterator(fileStream, (String) null); while (it.hasNext()) { buffer.add(it.nextLine()); } } catch (FileNotFoundException e) { log.error("File not found: " + pathToFile); throw new RuntimeException(e); } catch (IOException e) { log.error("Could not read from: " + pathToFile); throw new RuntimeException(e); } finally { org.apache.commons.io.IOUtils.closeQuietly(fileStream); } // Construct a string from the buffered lines StringBuilder sb = new StringBuilder(); for (Object s : buffer) { sb.append(s); sb.append(System.getProperty("line.separator")); } return sb.toString(); }
From source file:automaticdatabaseupdate.FileHandler.java
/** * * @param strFilePath - path of given file e.g. "D:\\EGYETEM\\Szakdolgozat\\Mernoki_tervezes\\update files\\sr28upd\\ADD_NUTR.txt" * @param strFileType - type of the given file e.g. AddFood * @param TList - list to fill with data e.g. List<FileFoodStruct> ListFFS *//* w w w . ja v a 2 s . c om*/ public static <T> void readFile(String strFilePath, List<T> TList, String strFileType) { TList.clear(); try { File file = FileUtils.getFile(strFilePath); LineIterator iter = FileUtils.lineIterator(file); while (iter.hasNext()) { String strLine = iter.next(); switch (strFileType) { case "ADD_FOOD": { ArrayList<String> strList = ParseUpdateFile(strLine); FileFoodStruct Object = new FileFoodStruct(Integer.valueOf(strList.get(0)), strList.get(2), strList.get(3), Integer.valueOf(strList.get(8))); TList.add((T) Object); System.out.println(Object.toString()); break; } case "ADD_NUTR": { ArrayList<String> strList = ParseUpdateFile(strLine); FileNutrientStruct Object = new FileNutrientStruct(Integer.valueOf(strList.get(0)), Integer.valueOf(strList.get(1)), Double.valueOf(strList.get(2))); TList.add((T) Object); System.out.println(Object.toString()); break; } case "ADD_WGT": { ArrayList<String> strList = ParseUpdateFile(strLine); FileWeightStruct Object = new FileWeightStruct(Integer.valueOf(strList.get(0)), Double.valueOf(strList.get(2)), strList.get(3), Double.valueOf(strList.get(4))); TList.add((T) Object); System.out.println(Object.toString()); break; } case "CHG_FOOD": { ArrayList<String> strList = ParseUpdateFile(strLine); FileFoodStruct Object = new FileFoodStruct(Integer.valueOf(strList.get(0)), strList.get(2), strList.get(3), Integer.valueOf(strList.get(8))); TList.add((T) Object); System.out.println(Object.toString()); break; } case "CHG_NUTR": { ArrayList<String> strList = ParseUpdateFile(strLine); FileNutrientStruct Object = new FileNutrientStruct(Integer.valueOf(strList.get(0)), Integer.valueOf(strList.get(1)), Double.valueOf(strList.get(2))); TList.add((T) Object); System.out.println(Object.toString()); break; } case "CHG_WGT": { ArrayList<String> strList = ParseUpdateFile(strLine); FileWeightStruct Object = new FileWeightStruct(Integer.valueOf(strList.get(0)), Double.valueOf(strList.get(2)), strList.get(3), Double.valueOf(strList.get(4))); TList.add((T) Object); System.out.println(Object.toString()); break; } case "DEL_FOOD": { ArrayList<String> strList = ParseUpdateFile(strLine); FileFoodStruct Object = new FileFoodStruct(Integer.valueOf(strList.get(0)), strList.get(1)); TList.add((T) Object); System.out.println(Object.toString()); break; } case "DEL_NUTR": { ArrayList<String> strList = ParseUpdateFile(strLine); FileNutrientStruct Object = new FileNutrientStruct(Integer.valueOf(strList.get(0)), Integer.valueOf(strList.get(1))); TList.add((T) Object); System.out.println(Object.toString()); break; } case "DEL_WGT": { ArrayList<String> strList = ParseUpdateFile(strLine); FileWeightStruct Object = new FileWeightStruct(Integer.valueOf(strList.get(0)), Double.valueOf(strList.get(2)), strList.get(3), Double.valueOf(strList.get(4))); TList.add((T) Object); System.out.println(Object.toString()); break; } case "LOG_FILE": { ArrayList<String> strList = ParseLogFile(strLine); TraceMessage Object = new TraceMessage(strList.get(0), strList.get(1)); TList.add((T) Object); break; } } } //System.out.println( "Total line: " + moduleCounter ); iter.close(); } catch (IOException ex) { Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:fr.inria.maestro.lga.graph.model.impl.nodenamer.NodeNamerImpl.java
private static int countLines(final File file, final String encoding) throws IOException { int count = 0; final LineIterator it = FileUtils.lineIterator(file, encoding); try {//w w w . j a va2 s . com while (it.hasNext()) { it.next(); count++; } } finally { it.close(); } return count; }