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

Updated on: 20-Feb-2020

237 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements