Hello community,
I've been trying to read a .txt file in Java. This is the line in my code that throws an error:
What I have tried:
Scanner in = new Scanner(Paths.get("C:\\Users\\longyuxi\\Documents\\ICPResult.txt"));
I've copied and pasted the directory into the file explorer on my computer and it opened the txt file that I wanted it to open. Nevertheless this line throws out a
java.io.IOException
.
I've also tried to read the file using FileReader. It throws out a
Java.io.FileNotFoundException
FileReader fileReader = new FileReader("C:\\Users\\longyuxi\\Documents\\ICPResult.txt");
Seeing that using absolute path doesn't work, I've tried to put my txt file under the working directory of my program. However I still found no luck. This is my code:
Scanner in2 = new Scanner(Paths.get("ICPResult.txt"));
Can someone please kindly point out my error?
Best regards
Update: I copied the file into different drives, but the code still doesn't work.
I've also added a try-catch block to the code that should print out the error message as follow. However, nothing is printed.
try {
File input = new File("ICPResult.txt");
FileReader fileReader = new FileReader("D:\\ICPResult.txt");
FileReader fileReader2 = new FileReader("F:\\ICPResult.txt");
Scanner in = new Scanner(Paths.get("C:\\Users\\longyuxi\\Documents\\ICPResult.txt"));
Scanner in2 = new Scanner(Paths.get("ICPResult.txt"));
}
catch(IOException ioe)
{
ioe.printStackTrace();
System.out.println(ioe.toString());
System.out.println(ioe.getMessage());
}