Presentation is loading. Please wait.

Presentation is loading. Please wait.

BUILDING JAVA PROGRAMS CHAPTER 6.4 FILE OUTPUT. 22 PrintStream : An object in the java.io package that lets you print output to a destination such as.

Similar presentations


Presentation on theme: "BUILDING JAVA PROGRAMS CHAPTER 6.4 FILE OUTPUT. 22 PrintStream : An object in the java.io package that lets you print output to a destination such as."— Presentation transcript:

1 BUILDING JAVA PROGRAMS CHAPTER 6.4 FILE OUTPUT

2 22 PrintStream : An object in the java.io package that lets you print output to a destination such as a file. Any methods you have used on System.out (such as print, println ) will work on a PrintStream. Syntax: PrintStream name = new PrintStream(new File(" file name ")); Example: PrintStream output = new PrintStream(new File("out.txt")); output.println("Hello, file!"); output.println("This is a second line of output."); OUTPUT TO FILES

3 33 DETAILS ABOUT PRINTSTREAM PrintStream name = new PrintStream(new File(" file name ")); If the given file does not exist, it is created. If the given file already exists, it is overwritten. The output you print appears in a file, not on the console. You will have to open the file with an editor to see it. Do not open the same file for both reading ( Scanner ) and writing ( PrintStream ) at the same time. You will overwrite your input file with an empty file (0 bytes).

4 44 SYSTEM.OUT AND PRINTSTREAM The console output object, System.out, is a PrintStream. PrintStream out1 = System.out; PrintStream out2 = new PrintStream(new File("data.txt")); out1.println("Hello, console!"); // goes to console out2.println("Hello, file!"); // goes to file A reference to it can be stored in a PrintStream variable. Printing to that variable causes console output to appear. You can pass System.out to a method as a PrintStream. Allows a method to send output to the console or a file.

5 55 We can ask the user to tell us the file to read. The filename might have spaces; use nextLine(), not next() // prompt for input file name Scanner console = new Scanner(System.in); System.out.print("Type a file name to use: "); String filename = console.nextLine(); Scanner input = new Scanner(new File(filename)); File s have an exists method to test for file-not-found: File file = new File("hours.txt"); if (!file.exists()) { // try a second input file as a backup System.out.print("hours file not found!"); file = new File("hours2.txt"); } PROMPTING FOR A FILE NAME

6 66 MIXING TOKENS AND LINES Using nextLine in conjunction with the token-based methods on the same Scanner can cause bad results. 23 3.14 Joe "Hello" world 45.219 You'd think you could read 23 and 3.14 with nextInt and nextDouble, then read Joe "Hello" world with nextLine. System.out.println(input.nextInt()); // 23 System.out.println(input.nextDouble()); // 3.14 System.out.println(input.nextLine()); // But the nextLine call produces no output! Why?

7 77 MIXING LINES AND TOKENS Don't read both tokens and lines from the same Scanner : 23 3.14 Joe "Hello world" 45.219 input.nextInt() // 23 23\t3.14\nJoe\t"Hello" world\n\t\t45.2 19\n ^ input.nextDouble() // 3.14 23\t3.14\nJoe\t"Hello" world\n\t\t45.2 19\n ^ input.nextLine() // "" (empty!) 23\t3.14\nJoe\t"Hello" world\n\t\t45.2 19\n ^ input.nextLine() // "Joe\t\"Hello\" world" 23\t3.14\nJoe\t"Hello" world\n\t\t45.2 19\n ^

8 88 LINE-AND-TOKEN EXAMPLE Scanner console = new Scanner(System.in); System.out.print("Enter your age: "); int age = console.nextInt(); System.out.print("Now enter your name: "); String name = console.nextLine(); System.out.println(name + " is " + age + " years old."); Log of execution (user input underlined): Enter your age: 12 Now enter your name: Mr. Black is 12 years old. Why? Overall input: 12\nMr. Black After nextInt(): 12\nMr. Black ^ After nextLine():12\nMr. Black ^

9 99 FORMATTING TEXT WITH PRINTF System.out.printf(" format string ", parameters ); A format string can contain placeholders to insert parameters: %d integer %f real number %s string these placeholders are used instead of + concatenation Example: int x = 3; int y = -17; System.out.printf("x is %d and y is %d!\n", x, y); // x is 3 and y is -17! printf does not drop to the next line unless you write \n

10 10 PRINTF WIDTH % W d integer, W characters wide, right-aligned %- W d integer, W characters wide, left-aligned % W f real number, W characters wide, right-aligned... for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 10; j++) { System.out.printf("%4d", (i * j)); } System.out.println(); // to end the line } Output: 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30

11 11 PRINTF PRECISION %. D f real number, rounded to D digits after decimal % W. D f real number, W chars wide, D digits after decimal %- W. D f real number, W wide (left-align), D after decimal double gpa = 3.253764; System.out.printf("your GPA is %.1f\n", gpa); System.out.printf("more precisely: %8.3f\n", gpa); Output: your GPA is 3.3 more precisely: 3.254 8 3

12 12 PRINTF QUESTION Modify our Receipt program to better format its output. Display results in the format below, with $ and 2 digits after. Example log of execution: How many people ate? 4 Person #1: How much did your dinner cost? 20.00 Person #2: How much did your dinner cost? 15 Person #3: How much did your dinner cost? 25.0 Person #4: How much did your dinner cost? 10.00 Subtotal: $70.00 Tax: $5.60 Tip: $10.50 Total: $86.10

13 13 PRINTF ANSWER (PARTIAL)... // Calculates total owed, assuming 8% tax and 15% tip public static void results(double subtotal) { double tax = subtotal *.08; double tip = subtotal *.15; double total = subtotal + tax + tip; // System.out.println("Subtotal: $" + subtotal); // System.out.println("Tax: $" + tax); // System.out.println("Tip: $" + tip); // System.out.println("Total: $" + total); System.out.printf("Subtotal: $%.2f\n", subtotal); System.out.printf("Tax: $%.2f\n", tax); System.out.printf("Tip: $%.2f\n", tip); System.out.printf("Total: $%.2f\n", total); }


Download ppt "BUILDING JAVA PROGRAMS CHAPTER 6.4 FILE OUTPUT. 22 PrintStream : An object in the java.io package that lets you print output to a destination such as."

Similar presentations


Ads by Google