File operators

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

Popular posts from this blog

PROFILE CREATING

First program

RESUME