Lab 30
AIM:  Write a program in Java to create, write, modify, read operations on a Text file.

FileExample.java
import java.io.*;
public class FileExample
{
    public static void main(String[] args)
    {
        try
        {
            // Creating new Text file
              File file = new File("C:/JavaProgram/FileExample.txt");
              if (!file.exists())
                {
                file.createNewFile();
                System.out.println("File created Successfully.");
                }
                FileOutputStream fout=new FileOutputStream("C:/JavaProgram/FileExample.txt");
                // Writing to new Text file
                String s="India is great.";
                byte b[]=s.getBytes();
                fout.write(b);
                fout.close();
                System.out.println("Writing Complete!");
                FileInputStream fin = new FileInputStream("C:/JavaProgram/FileExample.txt");
                
                //Reading from Text file
                int i;
                while((i=fin.read())!=-1)
                {
                    System.out.print((char)i);
                }
                fin.close();
                fout=new FileOutputStream("C:/JavaProgram/FileExample.txt");
                // Modifying Text file
                s="I love India.";
                b=s.getBytes();
                fout.write(b);
                fout.close();
                System.out.println("\n Modification Complete!");
                fin = new FileInputStream("C:/JavaProgram/FileExample.txt");
                //Reading from Text file
                while((i=fin.read())!=-1)
                {
                System.out.print((char)i);
                }
                fin.close();
                }
                    
                catch (IOException e)
                {
                    System.out.println("I/O Exception occurred.");
                }
        }
}

Output
file example output
file example in java by practical server


File is Created at specified path so open and check your message
file created at specified path
file example in java by practical server
 
Happy Coding :)

VISIT: https://codesnail.com