How to open a plain text file in Java?



You can access a plain text using the File class.

Example

Live Demo

import java.io.File;

public class ReadFile {
   public static void main(String[] args) {
      File f = null;
      String str = "data.txt";

      try {
         f = new File(str);
         boolean bool = f.canExecute();
         String a = f.getAbsolutePath();
         System.out.print(a);
         System.out.println(" is executable: "+ bool);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Output

 C:\Users\data is executable: true
Samual Sam
Samual Sam

Learning faster. Every day.


Advertisements