Input/Output (I/O) in Java refers to the process of reading data from a source (input) and writing data to a destination (output) Java provides several classes and methods to handle I/O operations, but for AP CSA, we’ll focus on the most common ones
System.out
for output System.out.println("Hello, World!"); // Prints and moves to a new line
System.out.print("No new line"); // Prints without moving to a new line
System.out.printf("Formatted output: %d", 42); // Prints formatted output
Scanner
and System.in
for input import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.println("Hello, " + name + "! You are " + age + " years old.");
scanner.close(); // Always close the Scanner when done
import java.io.PrintWriter;
import java.io.FileNotFoundException;
try {
PrintWriter writer = new PrintWriter("output.txt");
writer.println("This is a line of text.");
writer.printf("Formatted output: %d\n", 42); //\n is line break
writer.close();
} catch (FileNotFoundException e) {
System.out.println("Error: File not found");
}
import java.io.*;
public class FileIOExample {
public static void main(String[] args) {
// Writing to a file
try (FileWriter writer = new FileWriter("example.txt")) {
writer.write("Hello, File I/O in Java!");
} catch (IOException e) {
e.printStackTrace();
}
// Reading from a file
try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.print("new line\n"); // \n is line break
System.out.print("\"quote\""); //\" escape the double quote
System.out.print("\\"); // \\ will print a backslash
a) Scanner input = new Scanner();
b) Scanner input = new Scanner(System.in);
c) Scanner input = new Scanner(console);
d) Scanner input = System.in.getScanner();
e) Scanner input = new Scanner(System.out);
Scanner sc = new Scanner(System.in); int x = sc.nextInt(); String s = sc.nextLine(); System.out.println(s);
What will be printed if the user enters “42 Java” (without quotes) as input? a) 42 b) Java
c) 42 Java
d) An empty line
e) A runtime error will occur
Answer
(D is correct. After reading the integer 42 with nextInt(), the newline character is left in the input buffer. The subsequent nextLine() consumes this newline, resulting in an empty string being assigned to s.)
a) It always moves the cursor to a new line after printing
b) It can only print string values
c) It allows for formatted output using format specifiers
d) It is part of the Scanner class
What is the purpose of the following code?
PrintWriter writer = new PrintWriter("data.txt"); writer.close();
a) It reads data from a file named “data.txt”
b) It creates an empty file named “data.txt”
c) It deletes a file named “data.txt”
d) It appends data to an existing file named “data.txt”
e) It renames a file to “data.txt”Answer
(B is correct. This code creates a new empty file named "data.txt" or overwrites an existing file with that name.)
a)
double d = input.nextDouble();
b)double d = input.readDouble();
c)double d = input.getDouble();
d)double d = (double)input.next();
e)double d = Double.parseDouble(input.next());
a) ArrayIndexOutOfBoundsException
b) IOException
c) NullPointerException
d) ArithmeticException
Write a Java program that prompts the user to enter their name and age. Then, print a greeting message that includes the user’s name and age.
Write a Java method named calculateAverage that takes the path to a text file as an argument. This file contains numbers, one per line. The method should read these numbers, calculate their average, and print the result. Handle possible exceptions, and ensure resources are properly closed.