the file test.txt and the file Lautturi.class must be in the same directory:
/** * @author lautturi.com * Java example: cannot find file while reading text file */ import java.util.*; public class Lautturi { public static void main(String[] args) { Scanner scanner = new Scanner(Lautturi.class.getResourceAsStream("test.txt")); while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line); } scanner.close(); } }Soural.www:ecutturi.com
output:
The first line The second line