/* This program provides examples of Java IO using: * Keyboard input using Scanner * Keyboard input using BufferedReader * File input using BufferedReader * File input using Scanner * Need two input files * javaIO.txt for input from file using BufferReader * Newname.txt for input from file using Scanner * * Chuck Lillie * 10/31/2005 */ import java.util.*; import java.io.*; public class javaIO { // Use this to open a file for output static PrintStream p = FileOut(); /** * Routine to open a file to output data to the file homework5.txt */ public static PrintStream FileOut() { String filename = "output.txt"; // output.txt is a file in the same directory as this program // can have any filename. FileOutputStream out; // declare a file output object PrintStream p = null; // declare a print stream object try { // Create a new file output stream // connected to filename out = new FileOutputStream(filename); // Connect print stream to the output stream p = new PrintStream( out ); return (p); } catch (Exception e) { System.err.println ("Error writing to file"); return (p); } } // Start main program public static void main(String[] args) throws IOException { System.out.println("Program to illustrate various input capabilities."); p.println("Program to illustrate various input capabilities."); // Input from keyboard using BufferedReader System.out.println("\n***** Input from keyboard using BufferedReader *****"); p.println("\n***** Input from keyboard using BufferedReader *****"); BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); // Defines Buffered stream input as stdin System.out.print("Input an integer ending with Enter key: "); int bufInteger = Integer.parseInt(stdin.readLine()); System.out.print("Input a floating point number ending with Enter key: "); double bufDouble = Double.parseDouble(stdin.readLine()); System.out.print("Input a line of text ending with an Enter key: "); String bufString = stdin.readLine(); System.out.print("Input a single character ending with Enter key: "); char bufChar = (char)stdin.read(); System.out.println("" + bufInteger + ", " + bufDouble + ", " + bufString + ", " + bufChar); p.println("" + bufInteger + ", " + bufDouble + ", " + bufString + ", " + bufChar); // Input from keyboard using Scanner System.out.println("\n***** Input from keyboard using Scanner *****"); p.println("\n***** Input from keyboard using Scanner *****"); Scanner scanIn = new Scanner(System.in); // Defines the input stream as scanIn String delimiterScan = "\r\n"; scanIn.useDelimiter(delimiterScan); System.out.print("Input an integer ending with Enter key: "); int scanInteger = scanIn.nextInt(); System.out.print("Input a floating point number ending with Enter key: "); double scanDouble = scanIn.nextDouble(); System.out.print("Input a line of text ending with an Enter key: "); String scanString = scanIn.next(); System.out.print("Input a single character ending with Enter key: "); char scanChar = scanIn.next().charAt(0); System.out.println("" + scanInteger + ", " + scanDouble + ", " + scanString + ", " + scanChar); p.println("" + scanInteger + ", " + scanDouble + ", " + scanString + ", " + scanChar); // Input from a file using BufferedReader System.out.println("\n***** Input from file using BufferedReader *****"); p.println("\n***** Input from file using BufferedReader *****"); String filename = "javaIO.txt"; // Name of file to read -- can have any name File scanFile = new File(filename); // Name of file BufferedReader fileInB = new BufferedReader(new InputStreamReader(new FileInputStream(scanFile))); int fileBInteger; fileBInteger = Integer.parseInt(fileInB.readLine()); double fileBDouble = Double.parseDouble(fileInB.readLine()); String fileBString = fileInB.readLine(); char fileBChar = (char)fileInB.read(); System.out.println("" + fileBInteger + ", " + fileBDouble + ", " + fileBString + ", " + fileBChar); p.println("" + fileBInteger + ", " + fileBDouble + ", " + fileBString + ", " + fileBChar); fileInB.close(); // Input from a file using Scanner System.out.println("\n***** Input from file using Scanner *****"); p.println("\n***** Input from file using Scanner *****"); // String filename = "javaIO.txt"; // Name of file to read -- can have any name // File scanFile = new File(filename); // Name of file scanFile = new File("Newname.txt"); Scanner fileIn = new Scanner(scanFile); // Create fileIn to read from filename using Scanner String delimiter = "\r\n"; // need to set the delimiter to carriage return (\r) new line (\n) fileIn.useDelimiter(delimiter); // this sets delimiter so an entire line can be read int fileInteger; fileInteger = fileIn.nextInt(); double fileDouble = fileIn.nextDouble(); String fileString = fileIn.next(); char fileChar = fileIn.next().charAt(0); System.out.println("" + fileInteger + ", " + fileDouble + ", " + fileString + ", " + fileChar); p.println("" + fileInteger + ", " + fileDouble + ", " + fileString + ", " + fileChar); fileIn.close(); } }