Skip to main content

Posts

Showing posts with the label Unit 4 - Exceptions Error Handling and Basic IO (06)

Socket (TCP & UDP) communication in Java

Socket communication in Java enables communication between two endpoints over a network. There are two main types of sockets: TCP sockets and UDP sockets. Let's explain both types with examples: TCP Socket Communication: 1. **Server Side**:    - The server creates a `ServerSocket` object to listen for incoming connections on a specific port.    - When a client connects, the server accepts the connection and creates a `Socket` object to communicate with the client.    - The server reads from and writes to the socket's input and output streams to communicate with the client. import java.io.*; import java.net.*; public class TCPServer {     public static void main(String[] args) throws IOException {         ServerSocket serverSocket = new ServerSocket(12345);         System.out.println("Server started. Waiting for client...");         Socket clientSocket = serverSocket.accept();         System.out.println("Client connected.");         BufferedReader in = new Bu

Creating Files and Directories

File I/O Classes: Creating Files and Directories 1. File Class:    - Represents a file or directory path.    - Provides methods for creating new files and directories.    File file = new File("newFile.txt");    try {        if (file.createNewFile()) {            System.out.println("File created: " + file.getName());        } else {            System.out.println("File already exists.");        }    } catch (IOException e) {        System.out.println("An error occurred.");        e.printStackTrace();    } 2. mkdir() Method:    - Creates a directory.    File directory = new File("newDirectory");    if (directory.mkdir()) {        System.out.println("Directory created: " + directory.getName());    } else {        System.out.println("Directory already exists.");    } 3. mkdirs() Method:    - Creates a directory and its parent directories if they do not exist.    File directories = new File("newDirectories/childDirect

File I/O Classes Writing

File I/O Classes Writing 1. FileOutputStream:    - Writes raw bytes to a file output stream.    - Suitable for writing binary data to files.        try (FileOutputStream fos = new FileOutputStream("output.txt")) {        String data = "Hello, world!";        byte[] bytes = data.getBytes();        fos.write(bytes);    } catch (IOException e) {        e.printStackTrace();    } 2. BufferedWriter:    - Writes text to a character-output stream efficiently by buffering characters.        try (BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) {        String data = "Hello, world!";        bw.write(data);    } catch (IOException e) {        e.printStackTrace();    } 3. FileWriter:    - Writes character files using the default character encoding.        try (FileWriter fw = new FileWriter("output.txt")) {        String data = "Hello, world!";        fw.write(data);    } catch (IOException e) {        e.printStackTra

File I/O Classes Reading

File I/O Classes Reading  1. File Class:    - Represents a file or directory path in the file system.    - Provides methods for file manipulation and querying file attributes.        File file = new File("example.txt");    if (file.exists()) {        System.out.println("File exists!");    } else {        System.out.println("File does not exist!");    } 2. FileInputStream:    - Reads raw bytes from a file input stream.    - Suitable for reading binary data from files.        try (FileInputStream fis = new FileInputStream("example.txt")) {        int byteRead;        while ((byteRead = fis.read()) != -1) {            System.out.print((char) byteRead);        }    } catch (IOException e) {        e.printStackTrace();    } 3. BufferedReader:    - Reads text from a character-input stream efficiently by buffering characters.        try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) {        String line;        while (

Object Streams

 Object streams in Java are used to serialize and deserialize objects, allowing them to be written to and read from streams. Here are some notes and examples on object streams: 1. Object Streams:    - Object streams allow for the serialization and deserialization of Java objects.    - They provide a convenient way to persist Java objects to files or transmit them over networks. 2. ObjectInputStream and ObjectOutputStream:    - `ObjectInputStream` and `ObjectOutputStream` are classes in Java used for reading and writing objects to streams.    - These classes wrap byte streams and provide methods like `readObject()` and `writeObject()`.    Example (Writing Objects to ObjectOutputStream):    import java.io.*;    public class ObjectOutputStreamExample {        public static void main(String[] args) throws IOException {            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("objects.ser"));            Person person = new Person("John", 30);      

Data Streams

Data Streams in Java with examples: 1. Data Streams:    - Data streams are used for reading and writing primitive data types and strings from and to a source.    - They are more efficient than byte streams when working with primitive data types. 2. DataInputStream and DataOutputStream:    - `DataInputStream` and `DataOutputStream` are classes in Java that provide methods for reading and writing primitive data types and strings.    - These classes wrap byte streams and provide methods like `readInt()`, `writeDouble()`, `readUTF()`, etc.    Example (Writing to a DataOutputStream):    import java.io.*;    public class DataOutputStreamExample {        public static void main(String[] args) throws IOException {            DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));            dos.writeInt(42);            dos.writeDouble(3.14);            dos.writeUTF("Hello, world!");            dos.close();        }    }    Example (Reading from a Data

Scanning and Formatting

 Here are some notes on scanning and formatting in Java I/O with examples: 1. Scanning (Input):    - Scanning involves reading input from different sources like the keyboard, files, or network connections.    - Java provides the `Scanner` class in the `java.util` package to facilitate scanning.    Example:    import java.util.Scanner;    public class ScannerExample {        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. Formatting (Output):    - Formatting is the process of presenting data in a specific way, such as aligning text, setting precision for floating-point numbers, etc.    - The `System.out.printf()` method is commonly used for formatted output in Java I/O.    Example:    public class FormattingExample {    

Buffered Streams

Buffered Streams in Java: Buffered streams are used to improve the performance of input/output operations by reducing the number of system calls. They achieve this by using an internal buffer to read from or write to the underlying input/output stream in larger chunks, rather than one byte or character at a time. Buffered Input Stream Classes: - `BufferedInputStream`: Provides buffering for input bytes, allowing the reading of data from an underlying input stream. - `BufferedReader`: Reads text from a character-input stream, buffering characters to provide efficient reading of characters, arrays, and lines. Buffered Output Stream Classes: - `BufferedOutputStream`: Provides buffering for output bytes, allowing the writing of data to an underlying output stream. - `BufferedWriter`: Writes text to a character-output stream, buffering characters to provide efficient writing of characters, arrays, and lines. Example: Reading from a File using BufferedReader: import java.io.BufferedReader; i

Character Streams

Character streams in Java are used for handling input and output of character data, making them suitable for text-based operations where character encoding matters. Unlike byte streams, which deal with raw binary data, character streams handle characters and automatically handle character encoding and decoding. Let's explore character streams with an example: Example: Reading and Writing Text Files Using Character Streams In this example, we'll create a program that reads the contents of a text file using a `FileReader` and writes them into another text file using a `FileWriter`. import java.io.*; public class CharacterStreamExample {     public static void main(String[] args) {         String sourceFile = "source.txt";         String destinationFile = "destination.txt";         try (FileReader reader = new FileReader(sourceFile);              FileWriter writer = new FileWriter(destinationFile)) {             int character;             while ((character = re

Byte Streams

 Java Byte Streams Byte streams in Java are used to handle input and output of raw binary data. They are suitable for handling low-level data such as images, audio, and binary files. Let's dive into an example to illustrate how byte streams work: Example: Copying a File Using Byte Streams In this example, we'll create a program that reads the contents of a source file and writes them into a destination file using byte streams. import java.io.*; public class ByteStreamExample {     public static void main(String[] args) {         String sourceFile = "source.txt";         String destinationFile = "destination.txt";         try (FileInputStream fis = new FileInputStream(sourceFile);              FileOutputStream fos = new FileOutputStream(destinationFile)) {             int byteRead;             while ((byteRead = fis.read()) != -1) {                 fos.write(byteRead);             }             System.out.println("File copied successfully!");      

I/O Streams

 Input/Output Streams in Java In Java, streams represent a sequence of data. Input streams are used for reading data from a source, while output streams are used for writing data to a destination. Types of Streams: 1. Byte Streams:    - Operate on bytes.    - Suitable for binary data.    - `InputStream` and `OutputStream` are the abstract classes for byte streams. 2. Character Streams:    - Operate on characters, internally converting them to bytes.    - Suitable for text data.    - `Reader` and `Writer` are the abstract classes for character streams. Commonly Used Byte Streams: - `FileInputStream` and `FileOutputStream`: For reading/writing from/to files. - `ByteArrayInputStream` and `ByteArrayOutputStream`: For reading/writing to byte arrays. - `DataInputStream` and `DataOutputStream`: For reading/writing primitive data types. - `ObjectInputStream` and `ObjectOutputStream`: For reading/writing Java objects. Commonly Used Character Streams: - `FileReader` and `FileWriter`: For reading

JUnit Testing Framework

 JUnit is a popular testing framework for Java programming language. It provides a simple and efficient way to write unit tests for your Java code, allowing you to ensure that your code functions as expected and continues to work correctly as you make changes. Key Features of JUnit: 1. Annotations:  JUnit uses annotations to identify methods that specify test cases and setup/teardown operations. Annotations include `@Test`, `@Before`, `@After`, `@BeforeClass`, and `@AfterClass`. 2. Assertions:  JUnit provides a set of assertion methods such as `assertEquals`, `assertTrue`, `assertFalse`, `assertNotNull`, etc., to validate expected outcomes of tests. 3. Test Runners:  JUnit includes various test runners that execute test cases and report results. The default runner is `BlockJUnit4ClassRunner`, but JUnit also supports parameterized tests and suites. 4. Parameterized Tests:  JUnit allows you to write parameterized tests using the `@ParameterizedTest` annotation, which enables you to run t

Custom Exceptions

In Java, you can create custom exceptions by extending the `Exception` class or one of its subclasses like `RuntimeException`. Custom exceptions are useful when you want to define your own exception types to represent specific error conditions in your application. Syntax for creating a custom exception: public class CustomException extends Exception {     // Constructors, methods, and additional members can be defined here } Example of a custom exception: public class InvalidAgeException extends Exception {     public InvalidAgeException(String message) {         super(message);     } } public class AgeValidator {     public static void validateAge(int age) throws InvalidAgeException {         if (age < 0) {             throw new InvalidAgeException("Age cannot be negative");         }         if (age < 18) {             throw new InvalidAgeException("Must be at least 18 years old");         }         System.out.println("Age is valid");     }     pub

Chained Exceptions

 Chained exceptions, also known as nested exceptions, allow you to associate one exception with another. This feature is useful when you want to provide more context or information about the cause of an exception. In Java, you can chain exceptions using constructors that take a `Throwable` (or its subclasses) as an argument. Syntax: try {     // Code that may throw an exception } catch (ExceptionType1 e1) {     throw new ExceptionType2("Additional information", e1); } Explanation: - Inside a `catch` block, you can create a new exception object and pass the original exception (`e1`) as the cause. - The chained exception (`ExceptionType2`) includes a message and the original exception (`e1`) as its cause. - This technique allows you to preserve the original exception's stack trace and context while providing additional information about the higher-level exception. - Chained exceptions can be caught and processed at higher levels of the call stack, allowing for better error

Throwing Exceptions

 In Java, the `throw` statement is used to explicitly throw an exception within a method or block of code. It is typically used when an exceptional condition occurs that the method cannot handle itself, and it wants to signal to the calling code that something went wrong. Syntax: throw throwableObject; Explanation: - The `throw` statement requires an argument, which is usually an instance of a subclass of `Throwable` (either an exception or an error). - The argument passed to `throw` can be any expression that results in a throwable object. - When a `throw` statement is executed, the program flow is immediately transferred out of the current method or block and back to the calling code. - The caller can then catch the thrown exception using a `try-catch` block or propagate it further up the call stack. Example: public class AgeValidator {     public static void validateAge(int age) {         if (age < 0) {             throw new IllegalArgumentException("Age cannot be negative&q

The finally Block

 The `finally` block in Java is used to define a block of code that will be executed regardless of whether an exception is thrown or not within the `try` block. It provides a way to perform cleanup tasks such as closing resources, releasing locks, or finalizing operations. Syntax: try {     // Code that may throw an exception } catch (ExceptionType ex) {     // Code to handle the exception } finally {     // Code to be executed regardless of whether an exception occurs or not } Explanation: - The `finally` block is optional and can appear after the `try` block or after the last `catch` block. - The code inside the `finally` block will always be executed, irrespective of whether an exception occurs in the `try` block or whether it's caught by a `catch` block. - The `finally` block is often used to release resources that were acquired in the `try` block, ensuring they are properly released even if an exception occurs. - If an exception occurs in the `try` block and is caught by a `ca

The catch Blocks

 The `catch` blocks in Java are used to handle exceptions that occur within a `try` block. Each `catch` block specifies the type of exception it can handle, allowing you to provide different handling logic for different types of exceptions. Syntax: try {     // Code that may throw an exception } catch (ExceptionType1 ex1) {     // Code to handle ExceptionType1 } catch (ExceptionType2 ex2) {     // Code to handle ExceptionType2 } finally {     // Cleanup code (optional) } ``` Explanation: - Each `catch` block specifies the type of exception it can handle, denoted by `ExceptionType`. If an exception of that type (or a subtype) occurs in the `try` block, the corresponding `catch` block is executed. - You can have multiple `catch` blocks to handle different types of exceptions. - The order of `catch` blocks is important, as Java checks them sequentially from top to bottom. It's recommended to catch subclasses before their superclasses to avoid unreachable code warnings. - If an excepti

The `try` block in Java

The `try` block in Java is used to enclose the code that may throw an exception. It is followed by one or more `catch` blocks and an optional `finally` block. The `try` block allows you to handle exceptions gracefully by providing a mechanism to catch and handle any exceptions that occur during the execution of the enclosed code. Syntax: try {     // Code that may throw an exception } catch (ExceptionType1 ex1) {     // Code to handle ExceptionType1 } catch (ExceptionType2 ex2) {     // Code to handle ExceptionType2 } finally {     // Cleanup code (optional) } Explanation: - The `try` block encloses the code that you want to monitor for exceptions. - If an exception occurs within the `try` block, the control is transferred to the appropriate `catch` block that matches the type of the thrown exception. - You can have multiple `catch` blocks to handle different types of exceptions. - The `finally` block, if present, is executed regardless of whether an exception occurred or not. It is co

Catching and Handling Exceptions

Catching and handling exceptions is a crucial aspect of Java programming, as it allows developers to gracefully manage unexpected errors and prevent program termination. This is typically achieved using `try`, `catch`, and `finally` blocks. Let's explore each of these blocks and how they are used to catch and handle exceptions: 1. `try` Block: The `try` block contains the code that may potentially throw an exception. It is followed by one or more `catch` blocks to handle specific types of exceptions, or by a `finally` block for cleanup code. 2. `catch` Block: A `catch` block is used to catch and handle exceptions that are thrown within the corresponding `try` block. It specifies the type of exception it can handle and provides code to handle the exception. 3. `finally` Block: The `finally` block is optional and is used to execute cleanup code that should be run whether an exception occurs or not. This block is commonly used to release resources such as file handles or database conn

Exceptions and Errors

Exceptions: Exceptions represent exceptional conditions that can occur during the execution of a program. They can be caused by various factors such as invalid input, network issues, or file not found. Exceptions are further categorized into two types: 1. Checked Exceptions: These are exceptions that are checked at compile time. They are typically recoverable and should be handled by the programmer. Examples include `IOException`, `SQLException`, etc. 2. Unchecked Exceptions (Runtime Exceptions): These are exceptions that are not checked at compile time. They usually indicate programming errors or unexpected conditions. Examples include `NullPointerException`, `ArrayIndexOutOfBoundsException`, etc. Errors: Errors, on the other hand, represent serious problems that are typically beyond the control of the program. They are usually caused by system-level issues or conditions such as out of memory, stack overflow, or hardware failures. Errors are not normally caught or handled by the pro