Output Processing
PrintStream
- System.out
- Files
Output Processing PrintStream System.out Files Output Files - - PowerPoint PPT Presentation
Output Processing PrintStream System.out Files Output Files PrintStream Class System.out is a PrintStream! We generally use it to output to the console: System.out.println( Hello W orld!) ; System.out.print (E nter name:
import java.util.*; import java.io.*; public class PrintWords { public static void main(String [] args) throws FileNotFoundException { PrintStream output = new PrintStream(new File("words.txt"));
}
import java.util.*; import java.io.*; public class JingleWriter { public static void main(String[] args) { try { File file = new File("jingle.txt"); PrintStream output = new PrintStream(file);
} catch (FileNotFoundException e) { System.out.println("Problem creating file!"); } } }
/** * Returns a PrintStream for output to a file. NOTE: If file exists, this * code will overwrite the existing file. It is likely that you want to add * additional tests. * * @param console Scanner for console. * @return PrintStream for output to a file */ public static PrintStream getOutputPrintStream(Scanner console) { PrintStream output = null; while (output == null) { System.out.print("output file name? "); String name = console.next(); try {
} catch (FileNotFoundException e) { System.out.println("File unable to be written. Please try again."); } } return output; }
PrintStream output = null; while (output == null) { System.out.print("Enter output file name: "); File file = new File(console.next()); try { if (!file.exists()) {
} else { System.out.print ("File already exists, do you want to overwrite it (y/n)?"); String reply = console.next(); if (reply.equals("y") || reply.equals("Y")) {
} } } catch (FileNotFoundException e) { System.out.println(“File unable to be written: " + e); System.exit(1); } }
import java.util.*; import java.io.*; public class HamletCapitalize { public static void main(String[] args) { try { Scanner input = new Scanner(new File("hamlet.txt")); File file = new File("hamletCaps.txt"); PrintStream output = new PrintStream(file); while(input.hasNextLine()) {
} input.close(); // Close the Scanner
} catch (FileNotFoundException e) { System.out.println("Error: "+ e); } } }
import java.util.*; import java.io.*; public class HamletCapitalize { public static void main(String[] args) { Scanner input = null; PrintStream output = null; try { input = new Scanner(new File("hamlet.txt")); } catch (FileNotFoundException e) { System.out.println("hamlet.txt does not exist."); System.exit(1); } File file = new File("hamletCaps.txt"); try {
} catch (FileNotFoundException e) { System.out.println("hamletCaps.txt unable to be written: "+ e); System.exit(1); } while(input.hasNextLine()) {
} input.close(); // Close the Scanner
} }
12
23 3.14 Joe "Hello" world 45.2 19
– 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()); //
23 3.14 Joe "Hello world" 45.2 19 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 ^
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.");
Enter your age: 12 Now enter your name: Sideshow Bob is 12 years old.
– Overall input: 12\nSideshow Bob – After nextInt(): 12\nSideshow Bob ^ – After nextLine(): 12\nSideshow Bob
Scanner console = new Scanner(System.in); System.out.print("Enter your age: "); int age = console.nextInt();
//Add an extra console.nextLine() to read \n
console.nextLine();
System.out.print("Now enter your name: "); String name = console.nextLine(); System.out.println(name + " is " + age + " years old.");
Enter your age: 12 Now enter your name: Sideshow Bob Sideshow Bob is 12 years old.
Write a program called WeatherReporter that:
and uses the processLine() method as defined below to
public static void processLine(String line, PrintStream output) { } The output should be formatted as shown below: 01/01/2010 Low: 34.0 High: 48.0 Rain: no Snow: no 01/02/2010 Low: 24.1 High: 48.0 Rain: no Snow: no ... 12/26/2010 Low: 28.4 High: 36.0 Rain: yes Snow: yes 12/27/2010 Low: 21.2 High: 37.4 Rain: no Snow: no ...
the output file exists (just overwrite it).
file PrintStream.
PrintStream, output an error message to the console and exit.
YYYYMMDD Average(F) High(F) Low(F) Fog/Rain/Snow/Hail/Thunder/Tornado 20100101 44.2 48 34 100000 20100102 29.9 48 24.1 000000 20100103 24.2 32 18 000000 20100104 26.2 36 17.1 000000 20100105 26.9 36 17.1 000000 20100106 29.1 41 18 000000 20100107 32.4 48 19 000000 20100108 35.1 48 20.1 010000 20100109 25.9 35.6 19.4 000000
1 (yes) or 0 (no)
In a class named OneSpace, write a main() method that: – Prompts the user for the names of an input file and an output file. – Creates a file Scanner for the input file and a PrintStream associated with the
– Uses separate try/catch blocks for creating the Scanner and the PrintStream. – If a FileNotFoundException is caught, prints an appropriate error message and exit with a System.exit(1); – Calls the collapseSpaces method (defined below). – Closes the input Scanner and output PrintStream. Write a method named collapseSpaces that: – Accepts a Scanner representing an input file and a PrintStream representing an output file as its parameters, – Reads each line of the input file and outputs it to the output file with all its tokens separated by single spaces (collapsing any sequence of multiple spaces into a single space).
For example, consider the following text: many spaces on this line! If this text were a line in the input file, the same line should be written to the output file as follows: many spaces on this line! Hint: Use a line-based approach (create a line Scanner in collapseSpaces).