List of usage examples for java.io FileReader FileReader
public FileReader(FileDescriptor fd)
From source file:Main.java
public static void main(String[] args) throws Exception { int wordCount = 0, numberCount = 0; StreamTokenizer sTokenizer = new StreamTokenizer(new FileReader("yourFile.txt")); while (sTokenizer.nextToken() != StreamTokenizer.TT_EOF) { if (sTokenizer.ttype == StreamTokenizer.TT_WORD) wordCount++;/*w ww.ja v a2s . com*/ else if (sTokenizer.ttype == StreamTokenizer.TT_NUMBER) numberCount++; } System.out.println("Number of words in file: " + wordCount); System.out.println("Number of numbers in file: " + numberCount); }
From source file:Main.java
public static void main(String[] args) throws IOException { // create new reader FileReader fr = new FileReader("C:/test.txt"); LineNumberReader lnr = new LineNumberReader(fr); // reads and prints data from reader System.out.println((char) lnr.read()); System.out.println((char) lnr.read()); // mark invoked at this position lnr.mark(0);/*from w w w .j a va 2 s .c o m*/ System.out.println("mark() invoked"); System.out.println((char) lnr.read()); System.out.println((char) lnr.read()); // if this reader supports mark() an reset() if (lnr.markSupported()) { // reset() repositioned the reader to the mark lnr.reset(); System.out.println("reset() invoked"); System.out.println((char) lnr.read()); System.out.println((char) lnr.read()); } lnr.close(); }
From source file:Main.java
public static void main(String[] args) throws IOException { int i;/*from w w w . j av a 2s . co m*/ // create new reader FileReader fr = new FileReader("C:/test.txt"); LineNumberReader lnr = new LineNumberReader(fr); while ((i = lnr.read()) != -1) { // converts int to char char c = (char) i; // prints character System.out.println(c); } lnr.close(); }
From source file:Main.java
public static void main(String[] args) throws IOException { int i;/*from w ww . j ava 2 s. c o m*/ // create new reader FileReader fr = new FileReader("C:/test.txt"); LineNumberReader lnr = new LineNumberReader(fr, 200); // read till the end of the stream while ((i = lnr.read()) != -1) { // skip one byte lnr.skip(1); // converts integer to char char c = (char) i; // prints char System.out.print(c); } lnr.close(); }
From source file:MainClass.java
public static void main(String[] args) throws IOException { // 1. Reading input by lines: BufferedReader in = new BufferedReader(new FileReader("IOStreamDemo.java")); String s, s2 = new String(); while ((s = in.readLine()) != null) s2 += s + "\n"; in.close();//from w w w .j ava 2 s . c o m }
From source file:Main.java
public static void main(String[] args) throws Exception { BufferedReader in = null;//from ww w . ja v a 2s. co m in = new BufferedReader(new FileReader("fileeditor.txt")); StreamTokenizer st = new StreamTokenizer(in); st.eolIsSignificant(false); // remove comment handling st.slashSlashComments(false); st.slashStarComments(false); while (st.nextToken() != StreamTokenizer.TT_EOF) { if (st.ttype == StreamTokenizer.TT_NUMBER) { // the default is to treat numbers differently than words // also the numbers are doubles System.out.println((int) st.nval); } else { System.out.println(st.sval); } } }
From source file:Main.java
public static void main(String[] args) throws IOException { int i;/*from w w w . j a v a 2s.com*/ // create new reader FileReader fr = new FileReader("C:/test.txt"); LineNumberReader lnr = new LineNumberReader(fr); // read till the end of the stream while ((i = lnr.read()) != -1) { // skip one byte lnr.skip(1); // converts integer to char char c = (char) i; // prints char System.out.print(c); } lnr.close(); }
From source file:Main.java
public static void main(String[] args) throws IOException { String str;/*from w ww. j av a 2 s . com*/ // create new reader FileReader fr = new FileReader("C:/test.txt"); LineNumberReader lnr = new LineNumberReader(fr); // read lines till the end of the stream while ((str = lnr.readLine()) != null) { int i = lnr.getLineNumber(); System.out.print("(" + i + ")"); // prints string System.out.println(str); } lnr.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { BufferedReader br = new BufferedReader(new FileReader("in.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt")); int i;// ww w . j av a2 s. c o m do { i = br.read(); if (i != -1) { if (Character.isLowerCase((char) i)) bw.write(Character.toUpperCase((char) i)); else if (Character.isUpperCase((char) i)) bw.write(Character.toLowerCase((char) i)); else bw.write((char) i); } } while (i != -1); br.close(); bw.close(); }
From source file:MainClass.java
public static void main(String args[]) { try {// w w w.j a v a2 s . c om FileReader fr = new FileReader(args[0]); BufferedReader br = new BufferedReader(fr); StreamTokenizer st = new StreamTokenizer(br); st.ordinaryChar('.'); st.wordChars('\'', '\''); 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); } } fr.close(); } catch (Exception e) { System.out.println("Exception: " + e); } }