File Handling In JAVA

Java File Handling: In this video, we will see how files are handled in the Java programming language. In this video of our java complete course, we will see how java programming can be leveraged to read, write, create and delete files.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class tut15 {
    public static void main(String[] args) {
        // Create a file
//        File myFile = new File("Hello.txt");
//        try {
//            myFile.createNewFile();
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }

        // Write In a file

//        try {
//            FileWriter fileWriter = new FileWriter("Hello.txt");
//            fileWriter.write("Hello World, I am a Programmer.");
//            fileWriter.close();
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }

        // Read in a file

//        File myFile = new File("Hello.txt");
//        try {
//            Scanner sc = new Scanner(myFile);
//            while (sc.hasNextLine()) {
//                String line = sc.nextLine();
//                System.out.println(line);
//            }
//            sc.close();
//        } catch (FileNotFoundException e) {
//            throw new RuntimeException(e);
//        }

        // Delete a File

        File myFile = new File("Hello.txt");
        if (myFile.delete()){
            System.out.println("Deleted : " + myFile.getName() );
        }
        else System.out.println("Some error occurred");

    }
}

Leave a Reply

For News Subscribe Us!

Can curiosity may end shameless explained. True high on said mr on come. An do mr design at little myself wholly entire though. Attended of on stronger or mr pleasure.

You have been successfully Subscribed! Ops! Something went wrong, please try again.

© 2022 Code With AM