Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link

Input/Output Operations

A. File Handling in Java:

File handling is the process of working with files on a computer system. In Java, file handling can be achieved using the classes and methods provided in the java.io package.

Example: To create a new file, you can use the File class and its createNewFile() method. Here's an example:

import java.io.File;
import java.io.IOException;

public class FileHandlingExample {
    public static void main(String[] args) {
        try {
            File file = new File("example.txt");
            if (file.createNewFile()) {
                System.out.println("File created successfully!");
            } else {
                System.out.println("File already exists.");
            }
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

In this example, we create a new file called "example.txt" in the current working directory. The createNewFile() method returns a boolean value indicating whether the file was successfully created or not. If the file already exists, the method returns false.

B. Input and Output Streams in Java:

Input and output streams are used to read from and write to a file in Java. The java.io package provides several classes for working with streams, including FileInputStream, FileOutputStream, BufferedInputStream, and BufferedOutputStream.

Example: Here's an example that reads the contents of a file using a FileInputStream and outputs them to the console using a BufferedInputStream:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class InputStreamExample {
    public static void main(String[] args) {
        try {
            File file = new File("example.txt");
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(fis);
            byte[] contents = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = bis.read(contents)) != -1) {
                System.out.println(new String(contents, 0, bytesRead));
            }
            bis.close();
            fis.close();
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

In this example, we use a FileInputStream to read the contents of the file "example.txt". We then use a BufferedInputStream to buffer the input and improve performance. Finally, we output the contents of the file to the console.

C. Serialization and Deserialization in Java:

Serialization is the process of converting an object into a stream of bytes so that it can be stored on disk or transmitted over a network. Deserialization is the opposite process of converting a stream of bytes back into an object. Java provides built-in support for serialization and deserialization through the java.io.Serializable interface.

Example: Here's an example that demonstrates serialization and deserialization of an object:

import java.io.*;

public class SerializationExample {
    public static void main(String[] args) {
        try {
            // Serialize object to a file
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.ser"));
            MyClass obj = new MyClass("Hello World");
            oos.writeObject(obj);
            oos.close();
            
            // Deserialize object from file
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.ser"));
            MyClass newObj = (MyClass) ois.readObject();
            ois.close();
            
            // Output deserialized object
            System.out.println("Deserialized object: " + newObj.getMessage());
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

class MyClass implements Serializable {
    private String message;
    
    public MyClass(String message) {
        this.message = message;
    }
    
    public String getMessage() {
        return message;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
}

In this example, we define a class called MyClass that implements the Serializable interface. We then create an object of this class and serialize it to a file using an ObjectOutputStream. We then deserialize the object from the file using an ObjectInputStream, cast it back to its original type, and output its message to the console.

Post a Comment