Presentation is loading. Please wait.

Presentation is loading. Please wait.

September 9, 2008 Lecture 5 – More IO.

Similar presentations


Presentation on theme: "September 9, 2008 Lecture 5 – More IO."— Presentation transcript:

1 September 9, 2008 Lecture 5 – More IO

2 Alternatives Given: FileReader myFreader; One way Another way
myFreader = new FileReader(“MyFile.txt”); Another way String fileName; fileName = “MyFile.txt”; myFreader = new FileReader(fileName);

3 Alternatives Scanner fileScanner;
fileScanner = new Scanner (new File (“a.txt”));

4 Alternatives Scanner fileScanner; String fileName; fileName = “a.txt”;
fileScanner = new Scanner (new File (fileName));

5 Alternatives Scanner fileScanner; File myFile; String fileName;
fileName = “a.txt” // get from user myFile = new File (fileName); fileScanner = new Scanner (myFile);

6 Alternatives FileWriter fwriter; fwriter = new FileWriter (“b.txt”);
String fileName; fileName = “b.txt”; Fwriter = new FileWriter (fileName);

7 Alternatives PrintWriter myPrinter;
myPrinter = new PrintWriter (new FileWriter (“b.txt”));

8 Example import java.util.Scanner; public class Test { public static void main (String args []) { int number; Scanner keyboard; keyboard = new Scanner (System.in); System.out.println (" Hello "); System.out.println (" Please enter an integer and hit return "); while ( keyboard.hasNext()) { number = keyboard.nextInt (); System.out.println (" You entered " + number); } System.out.println (" Goodbye "); } }

9 Windows Screen Shot U:\Web\CS239\Lectures\Lecture5>java Test Hello
Please enter an integer and hit return 65 You entered 65 32 You entered 32 -235 You entered -235 ^Z Goodbye ^D Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at Test.main(Test.java:14) 53 You entered 53 U:\Web\CS239\Lectures\Lecture5>

10 Alternatives PrintWriter myPrinter; FileWriter myWriter;
myWriter = new FileWriter (“b.txt”); myPrinter = new PrintWriter (myWriter);

11 Alternatives PrintWriter myPrinter; FileWriter myWriter;
String fileName; fileName = “b.txt”; myWriter = new FileWriter (fileName); myPrinter = new PrintWriter (myWriter);

12 Alternatives End of file character Unix Windows <ctrl-d>
<ctrl-z>


Download ppt "September 9, 2008 Lecture 5 – More IO."

Similar presentations


Ads by Google