The `Scanner` class is used to read input from the user, such as text or numbers. You need to create a `Scanner` object and use it to capture input from the console.
Scanner scanner = new Scanner(System.in); System.out.print("Enter your name: "); String name = scanner.nextLine(); System.out.println("Hello, " + name + "!");
IOExceptions are errors that occur during input or output operations, such as reading from or writing to a file.
import java.io.FileReader; import java.io.IOException; public class Main { public static void main(String[] args) { try { FileReader file = new FileReader("file.txt"); } catch (IOException e) { System.out.println("An error occurred: " + e.getMessage()); } } }
Before running a Java program, it must be compiled into bytecode using the Java compiler (`javac`). This bytecode can then be executed by the Java Virtual Machine (JVM).
javac MyProgram.java java MyProgram
The `FileInputStream` class allows you to read bytes from a file. It is commonly used for file operations.
import java.io.FileInputStream; import java.io.IOException; public class Main { public static void main(String[] args) { // Using try-with-resources to automatically close the FileInputStream try (FileInputStream fileInput = new FileInputStream("file.txt")) { int data = fileInput.read(); while (data != -1) { System.out.print((char) data); // Cast to char to print as characters data = fileInput.read(); // Read next byte } } catch (IOException e) { // Handle any IOExceptions (file not found, reading error, etc.) System.out.println("An error occurred while reading the file: " + e.getMessage()); } } }
The `FileOutputStream` class allows you to write bytes to a file. You can use it to save data from your program into a file.
import java.io.FileOutputStream; import java.io.IOException; public class Main { public static void main(String[] args) { // Using try-with-resources to ensure that the FileOutputStream is closed automatically try (FileOutputStream fileOutput = new FileOutputStream("output.txt")) { // Write the string "Hello, World!" to the file fileOutput.write("Hello, World!".getBytes()); // Converts string to byte array } catch (IOException e) { // Handle any IOException (e.g., file write error) System.out.println("An error occurred while writing to the file: " + e.getMessage()); } } }
To print output to the console, you can use `System.out.println()`, `System.out.print()`, or `System.out.printf()` depending on your formatting needs.
System.out.println("This is a line of text."); System.out.print("This is text without a newline."); System.out.printf("Formatted number: %.2f", 3.14159);
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!