// File Input/Output Example import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class FileInputOutput { public static void main(String[] args) { int count = 1; try { // Open input stream Scanner scan = new Scanner(new File(args[0])); // Read contents while (scan.hasNextLine()) { // Read line String line = scan.nextLine(); // Print line System.out.println(count + ": " + line); count++; } // Close input stream scan.close(); } catch (IOException e) { System.out.println("Cannot read file: " + args[0]); System.exit(0); } // Open output file try { // Open output stream PrintWriter output = new PrintWriter(new File(args[1])); output.printf("Real number: %.2f\n", 123456.56789); output.println("Integer number: " + 987654); output.println("String variable: " + "Hello There"); // Close output stream output.close(); } catch (IOException e) { System.out.println("Cannot write file: " + args[1]); System.exit(0); } } }