Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

Java
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
Java
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.

Java
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());
        }
Posted
Updated 19-Jun-18 15:36pm
v2
Comments
Mohibur Rashid 19-Jun-18 20:52pm    
have you tried:
dir C:\Users\longyuxi\Documents\ICPResult.txt
in your command prompt? What does this says?
Yuxi Long 19-Jun-18 20:53pm    
Here's what I got:

C:\Users\longyuxi>dir C:\Users\longyuxi\Documents\ICPResult.txt
Volume in drive C is OS
Volume Serial Number is 9E72-6765

Directory of C:\Users\longyuxi\Documents

06/17/2018 05:00 PM 61,372 ICPResult.txt
1 File(s) 61,372 bytes
0 Dir(s) 8,098,463,744 bytes free
Mohibur Rashid 19-Jun-18 21:12pm    
Do me a favor. Run the following code and tell me what result do you get. Including entire error report
public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(Paths.get("C:\\Users\\longyuxi\\Documents\\ICPResult.txt"));
    System.out.println(in.nextLine());
    in.close();
  }
Yuxi Long 19-Jun-18 21:25pm    
I've run these lines and it printed out the first line of the file, no error message! I've added "throws IOException" to all the methods involved. Thank you, sir!

The code looks fine.

Would be good to see the full exception messages because they contain more information like "access denied".

Possible error sources are executing the application as another user (user is not allowed to access the file) or that the file is locked (file is opened by another application with exclusive access).
 
Share this answer
 
Comments
Yuxi Long 19-Jun-18 10:26am    
Hi,

Thank you so much for your response. I'm using an IDE and I've pulled out what I believe are the error messages:

Error:(30, 33) java: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
Error:(31, 22) java: unreported exception java.io.IOException; must be caught or declared to be thrown
Error:(32, 23) java: unreported exception java.io.IOException; must be caught or declared to be thrown
Jochen Arndt 19-Jun-18 10:35am    
You must catch the exceptions in your code using a try - catch block to get the full message (which is anyway a good idea).

However, have you ensured that the file is not opened by another application?
Yuxi Long 19-Jun-18 19:40pm    
I've updated my question. I've also copied the file into different locations, as well as temporarily disabling my antivirus software, just to be sure that no other program is blocking the file.
Richard MacCutchan 20-Jun-18 3:58am    
These are compiler messages telling you that you are not catching exceptions that could be thrown when you run the code. Your program has never even been compiled.
Yuxi Long 19-Jun-18 21:26pm    
Thanks to Mohibur Rashid, the problem has been solved. Thank you for your effort as well!
two ways to handle exception:

Java
public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(Paths.get("C:\\Users\\longyuxi\\Documents\\ICPResult.txt"));
    System.out.println(in.nextLine());
    in.close();
  }


Another example:
Java
public static void main(String[] args) {
    Scanner in = null;
    String firstLine=null;
    try {
    in = new Scanner(Paths.get("C:\\Users\\longyuxi\\Documents\\ICPResult.txt"));
    firstLine=in.nextLine();
    in.close();
    } catch(IOException e) {
     System.err.println("File not found");
    }
   if(firstLine!=null) {
    System.out.println(firstLine);
   }
  }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900