@Override public Reader getRankingExpression(String name) { File expressionFile = new File(searchDefinitionDir, name); try { return IOUtils.createReader(expressionFile, "utf-8"); } catch (IOException e) { throw new IllegalArgumentException("Could not read ranking expression file '" + expressionFile.getAbsolutePath() + "'", e); } }
@Override public Reader createReader() throws FileNotFoundException { try { if ( ! exists()) throw new FileNotFoundException("File '" + file + "' does not exist"); return IOUtils.createReader(file, "UTF-8"); } catch (IOException e) { throw new UncheckedIOException(e); } }
/** * 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); } }
/** * Reads a ranking expression from file to a string and returns it. * * @param name the name of the file to return, * relative to the search definition directory in the application package * @return the content of a ranking expression file * @throws IllegalArgumentException if the file was not found or could not be read */ @Override public Reader getRankingExpression(String name) { try { File file = expressionFileNameToFile(name); return IOUtils.createReader(expressionFileNameToFile(name), "utf-8"); } catch (IOException e) { throw new IllegalArgumentException("Could not read ranking expression file '" + name + "'", e); } }
/** * 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); } }