List of usage examples for java.io FileReader FileReader
public FileReader(FileDescriptor fd)
From source file:GetNameAsAttr.java
public static void main(String[] args) throws Exception { XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); Attr result = (Attr) xPath.evaluate("/schedule/@name", new InputSource(new FileReader("tds.xml")), XPathConstants.NODE); System.out.println(result.getValue()); result.setValue("The Colbert Report"); }
From source file:InputOutputDemo.java
public static void main(String[] a) throws Exception { PrintWriter pwr = new PrintWriter(new FileWriter("java2s.txt")); pwr.print(4711);//w ww . ja v a 2 s. c o m pwr.print(' '); pwr.print("Java Source and Support at www.java2s.com"); pwr.close(); StreamTokenizer stok = new StreamTokenizer(new FileReader("java2s.txt")); int tok = stok.nextToken(); while (tok != StreamTokenizer.TT_EOF) { System.out.println(stok.sval); tok = stok.nextToken(); } }
From source file:ScanMixed.java
public static void main(String args[]) throws IOException { int i;//from w w w. j a v a2 s . c o m 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 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 a va 2s . c om*/ FileReader fin = new FileReader("Test.txt"); Scanner src = new Scanner(fin); 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:Main.java
public static void main(String[] args) throws Exception { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.println(stdin.readLine()); BufferedReader in = new BufferedReader(new FileReader("Main.java")); String s, s2 = new String(); while ((s = in.readLine()) != null) s2 += s + "\n"; in.close();/*ww w. ja va2s . c o m*/ StringReader in1 = new StringReader(s2); int c; while ((c = in1.read()) != -1) System.out.print((char) c); BufferedReader in2 = new BufferedReader(new StringReader(s2)); PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out"))); int lineCount = 1; while ((s = in2.readLine()) != null) out1.println(lineCount++ + ": " + s); out1.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); NodeList shows = (NodeList) xPath.evaluate("/schedule/show", new InputSource(new FileReader("tds.xml")), XPathConstants.NODESET); for (int i = 0; i < shows.getLength(); i++) { Element show = (Element) shows.item(i); String guestName = xPath.evaluate("guest/name", show); System.out.println(guestName); System.out.println(show.getAttribute("weekday") + ", " + show.getAttribute("date")); }//from w w w.j a v a 2 s .c o m }
From source file:GuestList.java
public static void main(String[] args) throws Exception { XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); NodeList shows = (NodeList) xPath.evaluate("/schedule/show", new InputSource(new FileReader("tds.xml")), XPathConstants.NODESET); for (int i = 0; i < shows.getLength(); i++) { Element show = (Element) shows.item(i); String guestName = xPath.evaluate("guest/name", show); String guestCredit = xPath.evaluate("guest/credit", show); System.out.println(show.getAttribute("weekday") + ", " + show.getAttribute("date") + " - " + guestName + " (" + guestCredit + ")"); }/*ww w .ja v a2s . c o m*/ }
From source file:MainClass.java
public static void main(String args[]) throws Exception { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField nameTextField = new JTextField(); frame.add(nameTextField, BorderLayout.NORTH); FileReader reader = null;/* w ww .j a va 2 s . c om*/ try { reader = new FileReader("fileName.txt"); nameTextField.read(reader, "fileName.txt"); } catch (IOException exception) { System.err.println("Load oops"); exception.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException exception) { System.err.println("Error closing reader"); exception.printStackTrace(); } } } frame.setSize(250, 100); frame.setVisible(true); }
From source file:Test.java
public static void main(String[] args) throws Exception { final URL test = Test.class.getResource("/"); System.out.println(test);//from w ww .j a v a 2s . c o m final URL url = Test.class.getResource("/resources/test.txt"); System.out.println(url); if (url == null) { System.out.println("URL is null"); } else { final File file = new File(url.toURI()); final FileReader reader = new FileReader(file); final char[] buff = new char[20]; final int l = reader.read(buff); System.out.println(new String(buff, 0, l)); reader.close(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml")); xsr.nextTag(); // Advance to statements element TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); while (xsr.nextTag() == XMLStreamConstants.START_ELEMENT) { File file = new File("out/" + xsr.getAttributeValue(null, "account") + ".xml"); t.transform(new StAXSource(xsr), new StreamResult(file)); }//w w w. ja va 2 s . c o m }