Skip to main content

 File Operation


EXPLANATION : -   File operations involve interacting with files on a computer system, allowing you to create, read, write, and delete files. Common operations include:


1. Open a file – Access a file to read or modify it.

2. Read from a file– Extract data from the file.

3. Write to a file– Add or modify data in the file.

4. Close a file– End the interaction with the file to free up system resources.

5. Delete a file – Remove the file from the system. 


These actions help manage data in programs and applicationsapplications


PROGRAM:-


import java.io.File;


import java.io.*;

import java.io.FileWriter;

import java.io.FileReader;

import java.io.BufferedReader;

import java.io.IOException;


class FSC{

    

public static void main(String args[])throws IOException

{

int i;

DataInputStream d = new DataInputStream(System.in);

String fileName = "msc1.txt";

do

{

System.out.println("\nwhich type of file operation you want to perform?");

System.out.println("\n1.file creation\n2.read\n3.write\n4.update\n5.delete");

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

i= Integer.parseInt(d.readLine());

switch(i)

{

  case 1:

       createFile(fileName);

       break;

  case 2:

       readFromFile(fileName);

       break;

  case 3:

       writeToFile(fileName, "Hello!");

       break;

  case 4:

       updateFile(fileName, "\nThis is an appended line.");

       break;

 case 5:

       deleteFile(fileName);

       break;

 default:

      System.out.println("enter choice 1-4");

    

}

}while(i<=5);

}

public static void createFile(String fileName) 

{

 File file = new File(fileName);

try

{       

 if (file.createNewFile()) 

{

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

 } 

else {     

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

  }

}catch(Exception ex){}     

}

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

writer.flush();

writer.close();

        } 

catch (IOException e) {

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

  }

  }

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

writer.flush();

writer.close();

catch (IOException e) {  

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

       

 }

    }

   

 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);

            }

reader.close();

        } 

catch (IOException e) {

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

        }

    }

   

 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:

which type of file operation you want to perform?

1.file creation

2.read

3.write

4.update

5.delete

Enter your choice:

1

File created: msc1.txt


which type of file operation you want to perform?

1.file creation

2.read

3.write

4.update

5.delete

Enter your choice:

5

Deleted the file: msc1.txt


which type of file operation you want to perform?

1.file creation

2.read

3.write

4.update

5.delete

Enter your choice:

3

Successfully wrote to the file.


which type of file operation you want to perform?


1.file creation

2.read

3.write

4.update

5.delete

Enter your choice:

6

enter choice 1-4


Comments

Popular posts from this blog

Qvesys AI Summer Course 2026

Qvesys AI Summer Course 2026 | Kumbakonam | School & College Students QVESYS πŸ“ž Register Now Summer 2026 · Kumbakonam · Limited Seats BUILD YOUR FUTURE Qvesys Info-Tech presents an intensive summer tech training crafted for school & college students in Kumbakonam. Learn AI, Coding & more — from scratch to skill. πŸ“ž Call & Register — 8220015195 🌐 Visit qvesys.in 5 Expert Co...

Qvesys Summer Course 2026

Qvesys Summer Course 2026 | Kumbakonam | AI · Cybersecurity · Python · Data Science QVESYS πŸ“ž Register Now Summer 2026 · Kumbakonam · Limited Seats SECURE YOUR FUTURE Qvesys Info-Tech presents a summer tech training for school & college students in Kumbakonam. From Cybersecurity to AI — build real skills that employers want. πŸ“ž Call Now — 8220015195 🌐 qvesys.in ...

Email Marketing Automation & Lead Magnet Creation: Step-by-Step Side Project Template for Beginners

 Email Marketing Automation & Lead Magnet Creation: Step-by-Step Side Project Template for Beginners Introduction Email Marketing Automation and Lead Magnet Creation is one of the most powerful strategies to generate consistent leads and build a scalable online business. If you're looking to start a side project that generates income while helping businesses grow, this model is highly effective. In simple terms, you create valuable content (called lead magnets ) like ebooks, checklists, or reports. Businesses offer these for free in exchange for email addresses—helping them build their customer base. You can monetize this by offering lead magnet creation services, automation setup, and consulting . This guide will walk you step-by-step through creating your own Email Marketing Automation & Lead Magnet Creation side project . What is Email Marketing Automation & Lead Magnet Creation? Email Marketing Automation Email marketing automation is the process of sending e...