java how to read a text file

Java
import java.io.File;  // Import the File class
import java.io.FileNotFoundException;  // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files

public class ReadFile {
  public static void main(String[] args) {
    try {
      File myObj = new File("filename.txt");
      Scanner myReader = new Scanner(myObj);
      while (myReader.hasNextLine()) {
        String data = myReader.nextLine();
        System.out.println(data);
      }
      myReader.close();
    } catch (FileNotFoundException e) {
      System.out.println("An error occurred.");
      e.printStackTrace();
    }
  }
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

class Scratch{
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File("filename"));
        input.next();       //returns next String
        input.nextLine();   //returns next Line
        input.nextBoolean();//returns next Boolean
        input.nextInt();    //returns next Int
        input.nextDouble(); //returns next double
        ...
    }
}
Source

Also in Java: