Input and Output (I/O) Operations

Input and Output (I/O) operations in programming involve the interaction between a program and external sources or destinations of data, such as files, console, network connections, etc. In Java, there are several classes and mechanisms available for performing I/O operations.


Reading Input

1. Reading from Console

You can use the `Scanner` class to read input from the console.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter your name: ");

        String name = scanner.nextLine();
        System.out.println("Hello, " + name + "!");
        scanner.close();
    }
}


2. Reading from Files

You can use classes like `FileInputStream`, `BufferedReader`, or `Scanner` to read data from files.

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("input.txt");
            Scanner scanner = new Scanner(file);

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                System.out.println(line);
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            System.out.println("File not found: " + e.getMessage());
        }
    }
}


Writing Output

1. Writing to Console

You can use `System.out.println()` to print output to the console.

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}


2. Writing to Files

You can use classes like `FileOutputStream`, `BufferedWriter`, or `PrintWriter` to write data to files.

import java.io.File;
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!\n");
            writer.write("This is a Java program.");
            writer.close();

            System.out.println("Data written to file successfully.");
        } catch (IOException e) {
            System.out.println("An error occurred: " + e.getMessage());
        }
    }
}


Exception Handling

It's important to handle exceptions properly while performing I/O operations to deal with potential errors, such as file not found, permission denied, etc.


Closing Resources

Always ensure that you close the resources (like file streams, network connections) after their use to free up system resources and prevent resource leaks. Use `close()` method or `try-with-resources` statement for automatic resource management.

try (FileInputStream fis = new FileInputStream("file.txt")) {
   
// code to read from fis
} catch (IOException e) {
    // handle exception
}


I/O operations are fundamental to most applications, as they enable communication with users, storage, and retrieval of data from files or databases, and interaction with external systems.

Nenhum comentário:

Postar um comentário

Internet of Things (IoT) and Embedded Systems

The  Internet of Things (IoT)  and  Embedded Systems  are interconnected technologies that play a pivotal role in modern digital innovation....