I've been trying to read the text file in my java projects, I've been looking for the solution for the whole day, I've tried loads of methods but none of them have worked. Some of them: (Also, I have to use File and Scanner class)
String file = "fileTest.txt";
var path = Paths.get("test", file);
System.out.println(path);
System.out.println(Files.readString(path));
Exception in thread "main" java.nio.file.NoSuchFileException: test\fileTest.txt
URL url = ClassLoader.class.getResource("fileTest.txt");
File file = null;
file = new File(url.toURI());
Scanner scanner = new Scanner(file);
scanner.useDelimiter(" ");
while(scanner.hasNext()) {
System.out.println(scanner.toString());
}
Exception in thread "main" java.lang.NullPointerException
File file = new File("../test/fileTest.txt");
Scanner scanner = new Scanner(file);
scanner.useDelimiter(" ");
while(scanner.hasNext()) {
System.out.println(scanner.toString());
}
scanner.close();
Exception in thread "main" java.io.FileNotFoundException: ..\test\fileTest.txt (The system cannot find the path specified)