/** Convenience method for closing a list of readers. Does nothing if the given reader list is null. */ public static void closeAll(List<Reader> readers) { if (readers==null) return; for (Reader reader : readers) closeReader(reader); }
/** * Returns the number of lines in a file. * If the file does not exists, 0 is returned */ public static int countLines(String file) { BufferedReader reader = null; int lineCount = 0; try { reader = createReader(file,"utf8"); while (reader.readLine() != null) lineCount++; return lineCount; } catch (IOException e) { return lineCount; } finally { closeReader(reader); } }
/** * Returns a list containing the lines in the given file as strings * * @return a list of Strings for the lines of the file, in order * @throws IOException if the file could not be read */ public static List<String> getLines(String fileName) throws IOException { BufferedReader reader = null; try { List<String> lines = new java.util.ArrayList<>(); reader = createReader(fileName,"utf8"); String line; while (null != (line = reader.readLine())) lines.add(line); return lines; } finally { closeReader(reader); } }
/** * Copies the n first lines of a file to another file. * If the out file exists it will be overwritten * * @throws IOException if copying fails */ public static void copy(String inFile, String outFile, int lineCount) throws IOException { BufferedReader reader = null; BufferedWriter writer = null; try { reader = createReader(inFile); writer = createWriter(outFile, false); int c; int newLines = 0; while (-1 != (c=reader.read()) && newLines<lineCount) { writer.write(c); if (c=='\n') newLines++; } } finally { closeReader(reader); closeWriter(writer); } }
public RuleBase privateImportFile(String fileName,String automataFile,RuleBase ruleBase) throws IOException, ParseException { BufferedReader reader=null; try { reader= IOUtils.createReader(fileName, "utf-8"); File file=new File(fileName); String absoluteFileName=file.getAbsolutePath(); if (ruleBase==null) ruleBase=new RuleBase(); ruleBase.setName(stripLastName(file.getName())); privateImportFromReader(reader,absoluteFileName,automataFile,ruleBase); return ruleBase; } finally { IOUtils.closeReader(reader); } }