Input and Output in Java
- Published on
Introduction
Handling input and output (I/O) is a fundamental aspect of programming. In Java, various methods and classes facilitate data input from the user and data output to the console or files.
Standard Input and Output in Java
System.out.println()
The System.out.println()
method is commonly used to output data to the console.
System.out.println("Hello, World!");
Scanner Class
The Scanner
class from the java.util
package is widely used for reading input from the user.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
}
}
File Input and Output in Java
FileReader and FileWriter
Java offers FileReader and FileWriter classes for reading from and writing to files.
Reading from a File
import java.io.FileReader;
import java.io.BufferedReader;
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Writing to a File
import java.io.FileWriter;
import java.io.BufferedWriter;
public class Main {
public static void main(String[] args) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) {
bw.write("Hello, World!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Handling I/O Exceptions
I/O operations can throw exceptions, and handling them is crucial to prevent runtime crashes. Using try-catch blocks allows managing exceptions gracefully.
try {
// I/O operations
} catch (IOException e) {
e.printStackTrace();
}
Conclusion
Understanding and implementing input and output operations form the basis for interactive and dynamic applications in Java. Whether it's reading user input from the console, displaying messages, or managing file I/O, becoming proficient with Java's I/O mechanisms enhances your capability to build comprehensive applications.