File Handling

mail@pastecode.io avatar
unknown
java
a month ago
942 B
2
Indexable
Never
package readfile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.io.FileWriter;
public class Readfile 
{
    public static void main(String[] args) throws IOException
    {
        try
        {
            File obj=new File("theerdha2.txt");
            obj.createNewFile();
            FileWriter nw = new FileWriter("theerdha2.txt");
            nw.write("its a good file");
            Scanner sobj=new Scanner(obj);
            while(sobj.hasNextLine())
            {
                String data=sobj.next();
                System.out.println(data);
                sobj.close();
            }
        }
        catch(FileNotFoundException e)
        {
            System.out.println("file not found");
        }
        finally
        {
            System.out.println("file is successfully read");
        }
    }
    
}
Leave a Comment