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

Student Blog's

STUDENT BLOGS  S.NO NAME REGISTER NO BLOG LINK 1. AKASH .N P23CS366 1.  Product Calculation 2.  Question and Answer 3.  File Operation 4.  Profile 2. AMMU .M P23CS367 1.  Product Calculation 2.  Question and Answer 3.  File Operation 4.  Profile 3. ANBU .M P23CS368 1.  Product Calculation 2.  Question and Answer 3.  File Operation 4.  Profile 4. ANBUMANI .A P23CS369 1.  Product Calculation 2.  Question and Answer 3.  File Operation 4.  Profile 5. ARAVINDHAN .A P23CS370 1.  Product Calculation 2.  Question and Answer 3.  File Operation 4.  Profile 6. BARATH .M P23CS371 1...

PRACTICE TEST

                                             HTML ONLINE PRACTICE TEST           CSS ONLINE PRACTICE TEST          PLACEMENT APTITUDE PRACTICE TEST :01         PLACEMENT APTITUDE PRACTICE TEST :02

Java program

                                    Quiz   Program   import java.lang.*; import java.io.*; class Questions { public String [][]qpa; public String[][]qca;  Questions()throws IOException { qpa=new String[10][5]; /*questionsandobjectives*/ DataInputStream in=new DataInputStream(System.in); qpa[0][0]="How do you reverse a string in Java?";  qpa[0][1]="1.Using StringBuilder"; qpa[0][2]="2.Using StringBuffer";  qpa[0][3]="3.Using char array"; qpa[0][4]="4.Using recursion"; qpa[1][0]="How do you swap two numbers without using a third variable in Java?";  qpa[1][1]="1.Using Arithmetic Operators"; qpa[1][2]="2.Using Bitwise XOR Operator";  qpa[1][3]="3.Using Multiplication and Division"; qpa[1][4]="4.Using Addition and Subtraction"; qpa[2][0]="Which of the following option leads to the portability and security of Java?";  qpa[2][1]="1.Bytecode is executed by JVM"; qpa...