List of usage examples for java.io FileReader close
public void close() throws IOException
From source file:MainClass.java
public static void main(String args[]) throws IOException { int count = 0; double sum = 0.0; FileWriter fout = new FileWriter("test.txt"); fout.write("2, 3.4, 5,6, 7.4, 9.1, 10.5, done"); fout.close();/*from www. j av a2s . c om*/ FileReader fin = new FileReader("Test.txt"); Scanner src = new Scanner(fin); src.useDelimiter(", *"); while (src.hasNext()) { if (src.hasNextDouble()) { sum += src.nextDouble(); count++; } else { String str = src.next(); if (str.equals("done")) break; else { System.out.println("File format error."); return; } } } fin.close(); System.out.println("Average is " + sum / count); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { FileReader fr = new FileReader(args[0]); BufferedReader br = new BufferedReader(fr); StreamTokenizer st = new StreamTokenizer(br); 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://from w w w .java 2s . c om System.out.println(st.lineno() + ") " + (char) st.ttype); } } fr.close(); }
From source file:MainClass.java
public static void main(String args[]) { try {/*from w w w . j a va 2 s . c om*/ 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 Exception { FileReader fr = new FileReader(args[0]); BufferedReader br = new BufferedReader(fr); StreamTokenizer st = new StreamTokenizer(br); st.whitespaceChars(',', ','); 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:/*from w w w. j a va 2 s.com*/ System.out.println(st.lineno() + ") " + (char) st.ttype); } } fr.close(); }
From source file:TaskManager.java
public static void main(String[] args) { String configFileName = "tasks.conf"; if (args.length == 1) { configFileName = args[0];// w w w .j av a 2s. c o m } File configFile = new File(configFileName); if (!configFile.exists() || !configFile.canRead()) { System.err.println("Can't read config file '" + configFileName + "'"); System.exit(1); } FileReader configReader = null; try { configReader = new FileReader(configFile); } catch (FileNotFoundException fnfX) { } Tasks taskList = new Tasks(configReader); try { configReader.close(); } catch (IOException ioX) { } TaskManager ex = new TaskManager(taskList); }
From source file:ScanMixed.java
public static void main(String args[]) throws IOException { int i;/*from ww w. j a v a2 s . c om*/ double d; boolean b; String str; FileWriter fout = new FileWriter("test.txt"); fout.write("Testing Scanner 10 12.2 one true two false"); fout.close(); FileReader fin = new FileReader("Test.txt"); Scanner src = new Scanner(fin); while (src.hasNext()) { if (src.hasNextInt()) { i = src.nextInt(); System.out.println("int: " + i); } else if (src.hasNextDouble()) { d = src.nextDouble(); System.out.println("double: " + d); } else if (src.hasNextBoolean()) { b = src.nextBoolean(); System.out.println("boolean: " + b); } else { str = src.next(); System.out.println("String: " + str); } } fin.close(); }
From source file:MainClass.java
public static void main(String args[]) throws IOException { int i;// w ww. ja va 2 s. c o m double d; boolean b; String str; FileWriter fout = new FileWriter("test.txt"); fout.write("string true false 1 2 3 4.12"); fout.close(); FileReader fin = new FileReader("Test.txt"); Scanner src = new Scanner(fin); while (src.hasNext()) { if (src.hasNextInt()) { i = src.nextInt(); System.out.println("int: " + i); } else if (src.hasNextDouble()) { d = src.nextDouble(); System.out.println("double: " + d); } else if (src.hasNextBoolean()) { b = src.nextBoolean(); System.out.println("boolean: " + b); } else { str = src.next(); System.out.println("String: " + str); } } fin.close(); }
From source file:MainClass.java
public static void main(String args[]) throws IOException { int i;//from ww w .j av a2 s . com double d; boolean b; String str; FileWriter fout = new FileWriter("test.txt"); fout.write("Testing Scanner 10 12.2 one true two false"); fout.close(); FileReader fin = new FileReader("Test.txt"); Scanner src = new Scanner(fin); while (src.hasNext()) { if (src.hasNextInt()) { i = src.nextInt(); System.out.println("int: " + i); } else if (src.hasNextDouble()) { d = src.nextDouble(); System.out.println("double: " + d); } else if (src.hasNextBoolean()) { b = src.nextBoolean(); System.out.println("boolean: " + b); } else { str = src.next(); System.out.println("String: " + str); } } fin.close(); }
From source file:StreamTokenizerDemo.java
public static void main(String args[]) throws Exception { 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://w ww. j a v a 2 s . c o m System.out.println(st.lineno() + ") " + (char) st.ttype); } } fr.close(); }
From source file:Main.java
public static void main(String args[]) { try {//w w w . j a va 2 s. c o m 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_EOL) { 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); } }