try { BufferedReader br = new BufferedReader(new FileReader(inputFile)); ... ... } catch (Exception e) { e.printStacktrace(); }
try(BufferedReader br = new BufferedReader(new FileReader(file))) { for(String line; (line = br.readLine()) != null; ) { // process the line. } // line is not visible here. }
/** * Loads the data of the file specified. */ public String loadData() { String dataFileName = this.fileName; try (BufferedReader br = new BufferedReader(new FileReader(new File(dataFileName)))) { StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line).append('\n'); } this.loaded = true; return sb.toString(); } catch (Exception e) { LOGGER.error("File {} does not exist", dataFileName); } return null; }
/** * Reopen the file to reuse values. */ public synchronized void reloadFile() { try (Reader r = reader) { System.err.println("Reload " + filename); reader = new BufferedReader(new FileReader(filename)); } catch (IOException e) { throw new RuntimeException(e); } } }
static public void main (String[] args) throws Exception { Settings settings = null; String input = null, output = null, packFileName = "pack.atlas"; switch (args.length) { case 4: settings = new Json().fromJson(Settings.class, new FileReader(args[3])); case 3: packFileName = args[2]; case 2: output = args[1]; case 1: input = args[0]; break; default: System.out.println("Usage: inputDir [outputDir] [packFileName] [settingsFileName]"); System.exit(0); } if (output == null) { File inputFile = new File(input); output = new File(inputFile.getParentFile(), inputFile.getName() + "-packed").getAbsolutePath(); } if (settings == null) settings = new Settings(); process(settings, input, output, packFileName); } }
public void open(Map<String, Object> config, TopologyContext context, SpoutOutputCollector collector) { this.collector = collector; this.pending = new ConcurrentHashMap<UUID, Values>(); try { this.br = new BufferedReader(new FileReader(new File(this .filePath))); } catch (Exception ex) { ex.printStackTrace(); } }
LineNumberReader lnr = new LineNumberReader(new FileReader(new File("File1"))); lnr.skip(Long.MAX_VALUE); System.out.println(lnr.getLineNumber() + 1); //Add 1 because line index starts at 0 // Finally, the LineNumberReader object should be closed to prevent resource leak lnr.close();
private static String readFromOsRelease() throws Exception { try (FileReader fileReader = new FileReader(new File("/etc/os-release"))) { Properties properties = new Properties(); properties.load(fileReader); return unQuote(properties.getProperty("PRETTY_NAME")); } }
public static void main(String[] args) throws IOException { if (args.length < 3) { log.info("usage: XMLBeginEndIterator file element keepInternalBoolean"); return; } Reader in = new FileReader(args[0]); Iterator<String> iter = new XMLBeginEndIterator<>(in, args[1], args[2].equalsIgnoreCase("true")); while (iter.hasNext()) { String s = iter.next(); System.out.println("*************************************************"); System.out.println(s); } in.close(); }
private void readRawBytes(String fileName) throws IOException { BufferedReader in = new BufferedReader(new FileReader(fileName)); StringBuffer buf = new StringBuffer(); int c; while ((c = in.read()) >= 0) buf.append((char) c); mRawBuffer = buf.toString(); // System.out.println(mRawBuffer); in.close(); }
BufferedReader br = new BufferedReader(new FileReader("file.txt")); try { StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } String everything = sb.toString(); } finally { br.close(); }
public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new FileReader("/tmp/acstats")); Prior p = new Prior(br); HashSet<String> hs = new HashSet<String>(); hs.add("workshopname"); //hs.add("workshopacronym"); double d = p.get(hs); System.out.println("d is " + d); }
try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line; while ((line = br.readLine()) != null) { // process the line. } }
static public void main (String[] args) throws Exception { Settings settings = null; String input = null, output = null, packFileName = "pack.atlas"; switch (args.length) { case 4: settings = new Json().fromJson(Settings.class, new FileReader(args[3])); case 3: packFileName = args[2]; case 2: output = args[1]; case 1: input = args[0]; break; default: System.out.println("Usage: inputDir [outputDir] [packFileName] [settingsFileName]"); System.exit(0); } if (output == null) { File inputFile = new File(input); output = new File(inputFile.getParentFile(), inputFile.getName() + "-packed").getAbsolutePath(); } if (settings == null) settings = new Settings(); process(settings, input, output, packFileName); } }
@Test public void testFilteringBufferedReader() throws Exception { final String encoding = "UTF-8"; final String fileName = "LineIterator-Filter-test.txt"; final File testFile = new File(getTestDirectory(), fileName); final List<String> lines = createLinesFile(testFile, encoding, 9); final Reader reader = new BufferedReader(new FileReader(testFile)); this.testFiltering(lines, reader); }
@Override public Router getRouter(URL url) { try { // Transform File URL into Script Route URL, and Load // file:///d:/path/to/route.js?router=script ==> script:///d:/path/to/route.js?type=js&rule=<file-content> String protocol = url.getParameter(Constants.ROUTER_KEY, ScriptRouterFactory.NAME); // Replace original protocol (maybe 'file') with 'script' String type = null; // Use file suffix to config script type, e.g., js, groovy ... String path = url.getPath(); if (path != null) { int i = path.lastIndexOf('.'); if (i > 0) { type = path.substring(i + 1); } } String rule = IOUtils.read(new FileReader(new File(url.getAbsolutePath()))); // FIXME: this code looks useless boolean runtime = url.getParameter(Constants.RUNTIME_KEY, false); URL script = url.setProtocol(protocol).addParameter(Constants.TYPE_KEY, type) .addParameter(Constants.RUNTIME_KEY, runtime) .addParameterAndEncoded(Constants.RULE_KEY, rule); return routerFactory.getRouter(script); } catch (IOException e) { throw new IllegalStateException(e.getMessage(), e); } }
public static void main(String[] args) throws IOException { Reader in = new FileReader(args[0]); Tokenizer st = new NegraPennTokenizer(in); while (st.hasNext()) { String s = (String) st.next(); System.out.println(s); } }
public void readGrammar(String filename) { try { FileReader fin = new FileReader(filename); BufferedReader bin = new BufferedReader(fin); readGrammar(bin); bin.close(); fin.close(); } catch (IOException e) { throw new RuntimeIOException(e); } }
BufferedReader br = new BufferedReader(new FileReader(path)); try { return br.readLine(); } finally { br.close(); }
private static void printLog( String description, File file, PrintStream out ) { if ( file != null && file.exists() ) { out.println( String.format( "---------- BEGIN %s ----------", description ) ); try ( BufferedReader reader = new BufferedReader( new FileReader( file ) ) ) { reader.lines().forEach( out::println ); } catch ( IOException ex ) { out.println( "Unable to collect log files: " + ex.getMessage() ); ex.printStackTrace( out ); } finally { out.println( String.format( "---------- END %s ----------", description ) ); } } }