List of usage examples for java.io LineNumberReader readLine
public String readLine() throws IOException
From source file:com.aionemu.commons.scripting.AionScriptEngineManager.java
public void executeScriptList(File list) throws IOException { if (list.isFile()) { LineNumberReader lnr = new LineNumberReader(new FileReader(list)); String line;/* w w w. j a va 2 s. co m*/ File file; while ((line = lnr.readLine()) != null) { String[] parts = line.trim().split("#"); if (parts.length > 0 && !parts[0].startsWith("#") && parts[0].length() > 0) { line = parts[0]; if (line.endsWith("/**")) { line = line.substring(0, line.length() - 3); } else if (line.endsWith("/*")) { line = line.substring(0, line.length() - 2); } file = new File(SCRIPT_FOLDER, line); if (file.isDirectory() && parts[0].endsWith("/**")) { this.executeAllScriptsInDirectory(file, true, 32); } else if (file.isDirectory() && parts[0].endsWith("/*")) { this.executeAllScriptsInDirectory(file); } else if (file.isFile()) { try { this.executeScript(file); } catch (ScriptException e) { reportScriptFileError(file, e); } } else { log.warn("Failed loading: (" + file.getCanonicalPath() + ") @ " + list.getName() + ":" + lnr.getLineNumber() + " - Reason: doesnt exists or is not a file."); } } } lnr.close(); } else { throw new IllegalArgumentException( "Argument must be an file containing a list of scripts to be loaded"); } }
From source file:org.kuali.test.runner.execution.TestExecutionMonitor.java
private File getMergedPerformanceDataFile() { File retval = null;//w w w.j av a2 s . c o m PrintWriter pw = null; try { boolean headerWritten = false; for (TestExecutionContext tec : testExecutionList) { File f = tec.getPerformanceDataFile(); if (f != null) { if (pw == null) { int pos = f.getPath().lastIndexOf("_"); pw = new PrintWriter(retval = new File(f.getPath().substring(0, pos) + ".csv")); } LineNumberReader lnr = null; try { lnr = new LineNumberReader(new FileReader(f)); if (headerWritten) { lnr.readLine(); } String line = null; while ((line = lnr.readLine()) != null) { pw.println(line); } headerWritten = true; } catch (Exception ex) { LOG.error(ex.toString(), ex); } finally { if (lnr != null) { try { lnr.close(); FileUtils.deleteQuietly(f); } catch (Exception ex) { } ; } } } } } catch (Exception ex) { LOG.error(ex.toString(), ex); retval = null; } finally { if (pw != null) { pw.close(); } } return retval; }
From source file:io.fabric8.ConnectorMojo.java
private List<String> loadFile(InputStream fis) throws Exception { List<String> lines = new ArrayList<>(); LineNumberReader reader = new LineNumberReader(new InputStreamReader(fis)); String line;/*from w w w. j a v a 2 s. c o m*/ do { line = reader.readLine(); if (line != null) { lines.add(line); } } while (line != null); reader.close(); return lines; }
From source file:org.fbk.cit.hlt.dirha.Annotator.java
public List<Answer> classify(File fin) throws IOException { List<Answer> list = new ArrayList<>(); LineNumberReader lr = new LineNumberReader(new FileReader(fin)); String line = null;// w ww .j ava 2 s . co m int count = 0; String sentence_id = null; while ((line = lr.readLine()) != null) { try { if (line.length() == 0) continue; try { Double.parseDouble(line); sentence_id = line; } catch (NumberFormatException ex) { logger.debug(count + "\t" + line); String sid; if (sentence_id == null) sid = Integer.toString(count++); else { sid = sentence_id; sentence_id = null; } list.add(classify(line, sid)); } } catch (Exception e) { logger.error(e); } } return list; }
From source file:com.l2jfree.gameserver.scripting.L2ScriptEngineManager.java
public void executeScriptList(File list) throws IOException { if (list.isFile()) { LineNumberReader lnr = new LineNumberReader(new FileReader(list)); String line;/*from w w w . j av a 2 s . c o m*/ File file; while ((line = lnr.readLine()) != null) { String[] parts = line.trim().split("#"); if (parts.length > 0 && !parts[0].startsWith("#") && parts[0].length() > 0) { line = parts[0]; if (line.endsWith("/**")) { line = line.substring(0, line.length() - 3); } else if (line.endsWith("/*")) { line = line.substring(0, line.length() - 2); } file = new File(SCRIPT_FOLDER, line); if (file.isDirectory() && parts[0].endsWith("/**")) { this.executeAllScriptsInDirectory(file, true, 32); } else if (file.isDirectory() && parts[0].endsWith("/*")) { this.executeAllScriptsInDirectory(file); } else if (file.isFile()) { try { this.executeScript(file); } catch (ScriptException e) { reportScriptFileError(file, e); } } else { _log.warn("Failed loading: (" + file.getCanonicalPath() + ") @ " + list.getName() + ":" + lnr.getLineNumber() + " - Reason: doesnt exists or is not a file."); } } } lnr.close(); } else { throw new IllegalArgumentException( "Argument must be an file containing a list of scripts to be loaded"); } }
From source file:org.kalypso.kalypsomodel1d2d.sim.IterationInfoTelemac.java
@Override public void readIterFile() throws IOException { m_itrFile.refresh();// ww w . j ava 2s . co m if (!m_itrFile.exists()) return; /* Read file and write outputs */ LineNumberReader lnr = null; try { final byte[] content = FileUtil.getContent(m_itrFile); lnr = new LineNumberReader(new StringReader(new String(content, Charset.defaultCharset()))); while (lnr.ready()) { final String line = lnr.readLine(); if (line == null) break; processLine(line, lnr.getLineNumber()); } } catch (final FileNotFoundException e) { if (lnr == null) StatusUtilities.createStatus(IStatus.WARNING, ISimulation1D2DConstants.CODE_RMA10S, Messages.getString("org.kalypso.kalypsomodel1d2d.sim.IterationInfo.1"), e); //$NON-NLS-1$ final String msg = Messages.getString("org.kalypso.kalypsomodel1d2d.sim.IterationInfo.2", //$NON-NLS-1$ lnr.getLineNumber()); StatusUtilities.createStatus(IStatus.WARNING, ISimulation1D2DConstants.CODE_RMA10S, msg, e); } finally { IOUtils.closeQuietly(lnr); } }
From source file:org.kalypso.kalypsomodel1d2d.sim.IterationInfoSWAN.java
@Override public void readIterFile() throws IOException { m_itrFile.refresh();//from w ww .ja va2s .co m if (!m_itrFile.exists()) return; /* Read file and write outputs */ LineNumberReader lnr = null; try { final byte[] content = FileUtil.getContent(m_itrFile); lnr = new LineNumberReader(new StringReader(new String(content, Charset.defaultCharset()))); while (lnr.ready()) { final String line = lnr.readLine(); if (line == null) break; processLine(line, lnr.getLineNumber()); } } catch (final FileNotFoundException e) { // FIXME: these stati are never used; what happened here? // if( lnr == null ) // StatusUtilities.createStatus( IStatus.WARNING, ISimulation1D2DConstants.CODE_RMA10S, Messages.getString( "org.kalypso.kalypsomodel1d2d.sim.IterationInfo.1" ), e ); //$NON-NLS-1$ // // final String msg = Messages.getString( "org.kalypso.kalypsomodel1d2d.sim.IterationInfo.2", lnr.getLineNumber() ); //$NON-NLS-1$ // StatusUtilities.createStatus( IStatus.WARNING, ISimulation1D2DConstants.CODE_RMA10S, msg, e ); } finally { IOUtils.closeQuietly(lnr); } }
From source file:gpl.pierrick.brihaye.aramorph.InMemoryDictionaryHandler.java
/** Loads a dictionary into a <CODE>Set</CODE> where the <PRE>key</PRE> is entry and its <PRE>value</PRE> is a * <CODE>List</CODE> (each entry can have multiple values) * @param set The set/* w w w .j a v a2 s .com*/ * @param name A human-readable name * @param is The stream * @throws RuntimeException If a problem occurs when reading the dictionary */ private void loadDictionary(Map set, String name, InputStream is) throws RuntimeException { //TODO : should be static HashSet lemmas = new HashSet(); int forms = 0; String lemmaID = ""; System.out.print("Loading dictionary : " + name + " "); try { LineNumberReader IN = new LineNumberReader(new InputStreamReader(is, "ISO8859_1")); String line = null; while ((line = IN.readLine()) != null) { if ((IN.getLineNumber() % 1000) == 1) System.out.print("."); // new lemma if (line.startsWith(";; ")) { lemmaID = line.substring(3); // lemmaID's must be unique if (lemmas.contains(lemmaID)) throw new RuntimeException("Lemma " + lemmaID + "in " + name + " (line " + IN.getLineNumber() + ") isn't unique"); lemmas.add(lemmaID); } // comment else if (line.startsWith(";")) { } else { String split[] = line.split("\t", -1); //-1 to avoid triming of trail values //a little error-checking won't hurt : if (split.length != 4) { throw new RuntimeException("Entry in " + name + " (line " + IN.getLineNumber() + ") doesn't have 4 fields (3 tabs)"); } String entry = split[0]; // get the entry for use as key String vocalization = split[1]; String morphology = split[2]; String glossPOS = split[3]; String gloss; String POS; Pattern p; Matcher m; // two ways to get the POS info: // (1) explicitly, by extracting it from the gloss field: p = Pattern.compile(".*" + "<pos>(.+?)</pos>" + ".*"); m = p.matcher(glossPOS); if (m.matches()) { POS = m.group(1); //extract POS from glossPOS gloss = glossPOS; //we clean up the gloss later (see below) } // (2) by deduction: use the morphology (and sometimes the voc and gloss) to deduce the appropriate POS else { // we need the gloss to guess proper names gloss = glossPOS; // null prefix or suffix if (morphology.matches("^(Pref-0|Suff-0)$")) { POS = ""; } else if (morphology.matches("^F" + ".*")) { POS = vocalization + "/FUNC_WORD"; } else if (morphology.matches("^IV" + ".*")) { POS = vocalization + "/VERB_IMPERFECT"; } else if (morphology.matches("^PV" + ".*")) { POS = vocalization + "/VERB_PERFECT"; } else if (morphology.matches("^CV" + ".*")) { POS = vocalization + "/VERB_IMPERATIVE"; } else if (morphology.matches("^N" + ".*")) { // educated guess (99% correct) if (gloss.matches("^[A-Z]" + ".*")) { POS = vocalization + "/NOUN_PROP"; } // (was NOUN_ADJ: some of these are really ADJ's and need to be tagged manually) else if (vocalization.matches(".*" + "iy~$")) { POS = vocalization + "/NOUN"; } else POS = vocalization + "/NOUN"; } else { throw new RuntimeException( "No POS can be deduced in " + name + " (line " + IN.getLineNumber() + ")"); } } // clean up the gloss: remove POS info and extra space, and convert upper-ASCII to lower (it doesn't convert well to UTF-8) gloss = gloss.replaceFirst("<pos>.+?</pos>", ""); gloss = gloss.trim(); //TODO : we definitely need a translate() method in the java packages ! gloss = gloss.replaceAll(";", "/"); //TODO : is it necessary ? gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "A"); gloss = gloss.replaceAll("", "C"); gloss = gloss.replaceAll("", "E"); gloss = gloss.replaceAll("", "E"); gloss = gloss.replaceAll("", "E"); gloss = gloss.replaceAll("", "E"); gloss = gloss.replaceAll("", "I"); gloss = gloss.replaceAll("", "I"); gloss = gloss.replaceAll("", "I"); gloss = gloss.replaceAll("", "I"); gloss = gloss.replaceAll("", "N"); gloss = gloss.replaceAll("", "O"); gloss = gloss.replaceAll("", "O"); gloss = gloss.replaceAll("", "O"); gloss = gloss.replaceAll("", "O"); gloss = gloss.replaceAll("", "O"); gloss = gloss.replaceAll("", "U"); gloss = gloss.replaceAll("", "U"); gloss = gloss.replaceAll("", "U"); gloss = gloss.replaceAll("", "U"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "a"); gloss = gloss.replaceAll("", "c"); gloss = gloss.replaceAll("", "e"); gloss = gloss.replaceAll("", "e"); gloss = gloss.replaceAll("", "e"); gloss = gloss.replaceAll("", "e"); gloss = gloss.replaceAll("", "i"); gloss = gloss.replaceAll("", "i"); gloss = gloss.replaceAll("", "i"); gloss = gloss.replaceAll("", "i"); gloss = gloss.replaceAll("", "n"); gloss = gloss.replaceAll("", "o"); gloss = gloss.replaceAll("", "o"); gloss = gloss.replaceAll("", "o"); gloss = gloss.replaceAll("", "o"); gloss = gloss.replaceAll("", "o"); gloss = gloss.replaceAll("", "u"); gloss = gloss.replaceAll("", "u"); gloss = gloss.replaceAll("", "u"); gloss = gloss.replaceAll("", "u"); gloss = gloss.replaceAll("", "AE"); gloss = gloss.replaceAll("", "Sh"); gloss = gloss.replaceAll("", "Zh"); gloss = gloss.replaceAll("", "ss"); gloss = gloss.replaceAll("", "ae"); gloss = gloss.replaceAll("", "sh"); gloss = gloss.replaceAll("", "zh"); // note that although we read 4 fields from the dict we now save 5 fields in the hash table // because the info in last field, glossPOS, was split into two: gloss and POS DictionaryEntry de = new DictionaryEntry(entry, lemmaID, vocalization, morphology, gloss, POS); if (set.containsKey(entry)) { ((Collection) set.get(entry)).add(de); } else set.put(entry, de); forms++; } } IN.close(); System.out.println(); if (!"".equals(lemmaID)) System.out.print(lemmas.size() + " lemmas and "); System.out.println(set.size() + " entries totalizing " + forms + " forms"); } catch (IOException e) { throw new RuntimeException("Can not open : " + name); } }
From source file:org.opensextant.extractors.test.TestXCoord.java
/** * This will accomodate any test file that has at least the following style: * * FAMILY-XXX COORDINATE TEXT "FAIL"/*from w w w. j ava 2 s . c om*/ * * Where the first FAMILY token is * * @param coordfile */ public void fileTestByLines(String coordfile) { xcoord.match_UTM(true); xcoord.match_MGRS(true); xcoord.match_DD(true); xcoord.match_DMS(true); xcoord.match_DM(true); try { String _file = coordfile.trim(); String fname = FilenameUtils.getBaseName(_file); TestXCoordReporter tester = new TestXCoordReporter("./results/xcoord_" + fname + "-lines.csv"); java.io.LineNumberReader in = getLineReader(coordfile); String line = null; while ((line = in.readLine()) != null) { String text = line.trim(); if (text.startsWith("#")) { continue; } if (text.isEmpty()) { continue; } String fam = find_family(line); int famx = XConstants.get_CCE_family(fam); if (famx == XConstants.UNK_PATTERN) { log.error("Unknown test pattern TEXT=" + text); continue; } GeocoordTestCase tst = new GeocoordTestCase("#" + in.getLineNumber(), fam, text); TextMatchResult results = xcoord.extract_coordinates(tst.text, tst.id); /** * tst.family_id */ results.add_trace("Test Payload: " + tst.text); if (!results.evaluated) { continue; } log.info("=========FILE TEST " + tst.id + " FOUND:" + (results.matches.isEmpty() ? "NOTHING" : results.matches.size())); tester.save_result(tst, results); } tester.close_report(); log.info("=== FILE TESTS DONE ==="); } catch (Exception err) { log.error("TEST BY LINES", err); } }
From source file:com.bigdata.rdf.sail.webapp.AbstractTestNanoSparqlClient.java
/** * Read the contents of a file.//from ww w . j a va 2 s . c om * * @param file * The file. * @return It's contents. */ protected static String readFromFile(final File file) throws IOException { final LineNumberReader r = new LineNumberReader(new FileReader(file)); try { final StringBuilder sb = new StringBuilder(); String s; while ((s = r.readLine()) != null) { if (r.getLineNumber() > 1) sb.append("\n"); sb.append(s); } return sb.toString(); } finally { r.close(); } }