How to get the current working directory in Java?
Hello everyone! I just started learning Java and ran into a problem. I'm writing a program that should read a text file located in the same folder as my project. But when I run the code, the file is not found, even though I specified the correct name.
Here's my code:
```java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
File file = new File("data.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found!");
}
}
}
```
No answers yet.
Log in to leave an answer