File Operation

 FILE OPERATIONS

       In Java, file operations such as inserting, deleting, and modifying files can be performed using classes from the java.io and java.nio.file packages.

Java code :         

import java.io.file;

import java.io.*;

import java.util.Scanner;

class FS {

public static void main(String[] args) {

  Scanner scanner = new Scanner(System.in);

String fileName = "msc1.txt";


   int choice;


        do {


            System.out.println("\nFile Operations Menu:");


            System.out.println("1. Create a File");


            System.out.println("2. Write to the File");


            System.out.println("3. Read from the File");


            System.out.println("4. Update the File");


            System.out.println("5. Delete the File");


            System.out.print("Enter your choice: ");


            choice = scanner.nextInt();


            scanner.nextLine(); // consume the newline




            switch (choice) {


                case 1:


                    createFile(fileName);


                    break;


                case 2:


                    System.out.print("Enter content to write: ");


                    String content = scanner.nextLine();


                    writeToFile(fileName, content);


                    break;


                case 3:


                    readFromFile(fileName);


                    break;


                case 4:


                    System.out.print("Enter content to append: ");


                    String updateContent = scanner.nextLine();


                    updateFile(fileName, updateContent);


                    break;


                case 5:


                    deleteFile(fileName);


                    break;


                default:


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


            }


        } while (true);  // Continuous loop until the program is manually terminated




        // scanner.close();  // No need to close scanner since program will run indefinitely


    }




    public static void createFile(String fileName) {


        File file = new File(fileName);


        try {


            if (file.createNewFile()) {


                System.out.println("File created successfully: " + file.getName());


            } else {


                System.out.println("File already exists.");


            }


        } catch (IOException e) {


            System.out.println("An error occurred while creating the file.");


            e.printStackTrace();


        }


    }




    public static void writeToFile(String fileName, String content) {


        try (FileWriter writer = new FileWriter(fileName)) {


            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.printStackTrace();


        }


    }




    public static void updateFile(String fileName, String content) {


        try (FileWriter writer = new FileWriter(fileName, true)) {


            writer.write(content);


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


        } catch (IOException e) {


            System.out.println("An error occurred while updating the file.");


            e.printStackTrace();


        }


    }




    public static void readFromFile(String fileName) {


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


            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 from the file.");


            e.printStackTrace();


        }


    }




    public static void deleteFile(String fileName) {


        File file = new File(fileName);


        if (file.delete()) {


import java.io.*;

import java.util.Scanner;




class FS {


    public static void main(String[] args) {


        Scanner scanner = new Scanner(System.in);


        String fileName = "msc1.txt";


        int choice;




        do {


            System.out.println("\nFile Operations Menu:");


            System.out.println("1. Create a File");


            System.out.println("2. Write to the File");


            System.out.println("3. Read from the File");


            System.out.println("4. Update the File");


            System.out.println("5. Delete the File");


            System.out.print("Enter your choice: ");


            choice = scanner.nextInt();


            scanner.nextLine(); // consume the newline




            switch (choice) {


                case 1:


                    createFile(fileName);


                    break;


                case 2:


                    System.out.print("Enter content to write: ");


                    String content = scanner.nextLine();


                    writeToFile(fileName, content);


                    break;


                case 3:


                    readFromFile(fileName);


                    break;


                case 4:


                    System.out.print("Enter content to append: ");


                    String updateContent = scanner.nextLine();


                    updateFile(fileName, updateContent);


                    break;


                case 5:


                    deleteFile(fileName);


                    break;


                default:


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


            }


        } while (true); // Continuous loop until the program is manually terminated




        // scanner.close(); // No need to close scanner since program will run indefinitely


    }




    public static void createFile(String fileName) {


        File file = new File(fileName);


        try {


            if (file.createNewFile()) {


                System.out.println("File created successfully: " + file.getName());


            } else {


                System.out.println("File already exists.");


            }


        } catch (IOException e) {


            System.out.println("An error occurred while creating the file.");


            e.printStackTrace();


        }


    }




    public static void writeToFile(String fileName, String content) {


        try (FileWriter writer = new FileWriter(fileName)) {


            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.printStackTrace();


        }


    }




    public static void updateFile(String fileName, String content) {


        try (FileWriter writer = new FileWriter(fileName, true)) {


            writer.write(content);


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


        } catch (IOException e) {


            System.out.println("An error occurred while updating the file.");


            e.printStackTrace();


        }


    }




    public static void readFromFile(String fileName) {


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


            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 from the file.");


            e.printStackTrace();


        }


    }




    public static void deleteFile(String fileName) {


        File file = new File(fileName);


        if (file.delete()) {


            System.out.println("Deleted the file: " + file.getName());


        } else {


            System.out.println("Failed to delete the file.");

      }

    }

}

OUTPUT :-







Comments

Popular posts from this blog

LEETCODE PRACTICE

II-MSC ( file Operation)

Java - SpringBoot