0% found this document useful (0 votes)
2 views

java_io_for_beginners

The document introduces Input/Output (I/O) in Java, explaining the difference between text and binary data. It provides examples of reading input from the user using the Scanner class, writing output to the console, and handling file operations for reading and writing data. Additionally, it covers error handling with try/catch blocks and includes practice exercises for beginners.

Uploaded by

michaeltsemzang
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

java_io_for_beginners

The document introduces Input/Output (I/O) in Java, explaining the difference between text and binary data. It provides examples of reading input from the user using the Scanner class, writing output to the console, and handling file operations for reading and writing data. Additionally, it covers error handling with try/catch blocks and includes practice exercises for beginners.

Uploaded by

michaeltsemzang
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Input and Output in Java for Beginners

What is Input/Output (I/O)?


Input means taking data into your program, while output means sending data out of your
program.

Types of Input/Output (I/O)


Java deals with two main types of data when performing I/O:
1. Text (Characters): Letters, numbers, and symbols that we type or see.
2. Binary (Bytes): Everything else, like images, audio, and other files.

Basic Input/Output in Java


1. Reading Input from the User (from the Keyboard)
If we want to take input from the user, we can use a class called Scanner. Example:

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 + "!");
scanner.close();
}

2. Writing Output to the Console (the Screen)


Java uses System.out to send output to the console using System.out.println().

Input/Output with Files


1. Reading from a File
Here’s how you can read from a file in Java:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try {
File file = new File("example.txt");
Scanner reader = new Scanner(file);
while (reader.hasNextLine()) {
String data = reader.nextLine();
System.out.println(data);
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}

2. Writing to a File
Here’s how you can write data to a file in Java:

import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("output.txt");
writer.write("Hello, world!");
writer.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}

Handling Errors (Exceptions)


When working with files, things can go wrong. These issues are called exceptions, and you
need to handle them using try and catch blocks.

Summary of Concepts for Beginners:


- Input: Taking data from the user or a file.
- Output: Showing data on the screen or saving it to a file.
- Streams: Data flows in and out through "streams" (pipes).
- Scanner: Helps read input (from keyboard or files).
- System.out.println(): Outputs text to the console.
- File, FileWriter, and Scanner: Help read and write files.
- try/catch: Handles errors that may happen when working with files.

Practice:
- Write a program that takes the user’s name and saves it to a file.
- Then, write another program that reads the saved name from the file and prints it on the
screen.

You might also like