List of usage examples for java.io FileReader FileReader
public FileReader(FileDescriptor fd)
From source file:MainClass.java
public static void main(String args[]) { try {// w ww . j a va 2 s . c o m FileReader fr = new FileReader(args[0]); BufferedReader br = new BufferedReader(fr); StreamTokenizer st = new StreamTokenizer(br); // Consider end-of-line as a token st.eolIsSignificant(true); // Declare variable to count lines int lines = 1; // Process tokens while (st.nextToken() != StreamTokenizer.TT_EOF) { switch (st.ttype) { case StreamTokenizer.TT_EOL: ++lines; } } System.out.println("There are " + lines + " lines"); fr.close(); } catch (Exception e) { System.out.println("Exception: " + e); } }
From source file:Main.java
public static void main(String[] args) throws IOException { char[] cbuf = new char[5]; // create new reader FileReader fr = new FileReader("C:/test.txt"); LineNumberReader lnr = new LineNumberReader(fr); // read characters into the buffer int i = lnr.read(cbuf, 2, 3); System.out.println("Number of char read: " + i); // for each character in the buffer for (char c : cbuf) { // if char is empty if ((int) c == 0) c = '-'; // prints char System.out.print(c);/*from ww w. j a v a2 s .c o m*/ } lnr.close(); }
From source file:Unbuffered.java
public static void main(String args[]) { Reader reader;/*from www. ja v a 2s .c o m*/ int ch; System.out.println("Start! " + new Date()); try { reader = new FileReader(args[0]); while ((ch = reader.read()) != -1) { // read entire file } } catch (IOException io) { System.err.println(io); System.exit(-1); } System.out.println("Stop! " + new Date()); }
From source file:Main.java
public static void main(String[] arguments) throws Exception { StringBuffer output = new StringBuffer(); FileReader file = new FileReader("a.htm"); BufferedReader buff = new BufferedReader(file); boolean eof = false; while (!eof) { String line = buff.readLine(); if (line == null) eof = true;//from w ww. ja v a 2 s.c o m else output.append(line + "\n"); } buff.close(); String page = output.toString(); Pattern pattern = Pattern.compile("<a.+href=\"(.+?)\""); Matcher matcher = pattern.matcher(page); while (matcher.find()) { System.out.println(matcher.group(1)); } }
From source file:MainClass.java
public static void main(String args[]) { try {//from ww w . j ava 2 s . com // Create a file reader FileReader fr = new FileReader(args[0]); // Create a buffered reader BufferedReader br = new BufferedReader(fr); // Create a stream tokenizer StreamTokenizer st = new StreamTokenizer(br); // Process tokens while (st.nextToken() != StreamTokenizer.TT_EOF) { switch (st.ttype) { case StreamTokenizer.TT_WORD: System.out.println(st.lineno() + ") " + st.sval); break; case StreamTokenizer.TT_NUMBER: System.out.println(st.lineno() + ") " + st.nval); break; default: System.out.println(st.lineno() + ") " + (char) st.ttype); } } // Close file reader fr.close(); } catch (Exception e) { System.out.println("Exception: " + e); } }
From source file:Main.java
public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new FileReader("ravi.txt")); while (true) { String line = br.readLine(); if (line == null) break; if (!UCODE_PATTERN.matcher(line).matches()) { System.err.println("Bad input: " + line); } else {// w w w . j a v a2 s .co m String hex = line.substring(2, 6); int number = Integer.parseInt(hex, 16); System.out.println(hex + " -> " + ((char) number)); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader sr = factory.createXMLStreamReader(new FileReader("test.xml")); System.out.println(sr.getClass()); while (sr.hasNext()) { int eventType = sr.next(); if (eventType == XMLStreamReader.START_DOCUMENT) { continue; } else if (eventType == XMLStreamReader.END_ELEMENT) { System.out.println("End Element: " + sr.getLocalName()); } else if (eventType == XMLStreamReader.START_ELEMENT) { System.out.println("Start Element: " + sr.getLocalName()); }/*from ww w .j av a 2s. co m*/ } }
From source file:Main.java
public static void main(String[] argv) throws Exception { String filename = "infile.txt"; String patternStr = "pattern"; BufferedReader rd = new BufferedReader(new FileReader(filename)); Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher("\\D"); String line = null;//from w ww . ja v a 2 s. c o m while ((line = rd.readLine()) != null) { matcher.reset(line); if (matcher.find()) { // line matches the pattern } } }
From source file:Main.java
public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("js"); try {/* w w w.ja v a2s . c o m*/ FileReader reader = new FileReader("yourFile.js"); engine.eval(reader); reader.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String args[]) throws Exception { XMLInputFactory xmlif = XMLInputFactory.newInstance(); XMLEventReader xmler = xmlif.createXMLEventReader(new FileReader("yourFile.xml")); XMLEvent event;// w w w . ja va2s.c om while (xmler.hasNext()) { event = xmler.nextEvent(); if (event.isStartElement()) { System.out.println(event.asStartElement().getName()); } else if (event.isCharacters()) { System.out.println(event.asCharacters().getData()); } } }