File operations

 import java.io.*;

import java.util.Scanner;


public class FileOperations {


    private static final String FILE_PATH = "example.txt";


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int choice;


        while (true) {

            System.out.println("Choose an operation:");

            System.out.println("1. Read from file");

            System.out.println("2. Write to file");

            System.out.println("3. Update file");

            System.out.println("4. Delete file");

            System.out.println("5. Exit");


            choice = scanner.nextInt();

            scanner.nextLine();  // Consume newline


            switch (choice) {

                case 1:

                    readFile();

                    break;

                case 2:

                    System.out.println("Enter text to write to the file:");

                    String contentToWrite = scanner.nextLine();

                    writeFile(contentToWrite);

                    break;

                case 3:

                    System.out.println("Enter new content to update the file:");

                    String contentToUpdate = scanner.nextLine();

                    updateFile(contentToUpdate);

                    break;

                case 4:

                    deleteFile();

                    break;

                case 5:

                    System.out.println("Exiting the program.");

                    scanner.close();

                    return;

                default:

                    System.out.println("Invalid choice. Please select again.");

            }

        }

    }


    // Method to read from the file

    private static void readFile() {

        try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) {

            String line;

            System.out.println("Reading from file:");

            while ((line = reader.readLine()) != null) {

                System.out.println(line);

            }

        } catch (IOException e) {

            System.out.println("An error occurred while reading the file: " + e.getMessage());

        }

    }


    // Method to write to the file

    private static void writeFile(String content) {

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH))) {

            writer.write(content);

            System.out.println("Successfully wrote to the file.");

        } catch (IOException e) {

            System.out.println("An error occurred while writing to the file: " + e.getMessage());

        }

    }


    // Method to update the file

    private static void updateFile(String content) {

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH, true))) {

            writer.newLine();  // Add a newline before appending

            writer.write(content);

            System.out.println("Successfully updated the file.");

        } catch (IOException e) {

            System.out.println("An error occurred while updating the file: " + e.getMessage());

        }

    }


    // Method to delete the file

    private static void deleteFile() {

        File file = new File(FILE_PATH);

        if (file.delete()) {

            System.out.println("File deleted successfully.");

        } else {

            System.out.println("Failed to delete the file or file does not exist.");

        }

    }

}





Comments

Popular posts from this blog

Profile

Question Program