1

I posted a similar question here: Read from a file containing integers - java but couldn't get a decent reply.

Now I have written this new code which only reads a file and outputs the result.

I get a FileNotFoundException whenever I try to read from a file. The code is below:

import java.io.*;

public class second {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {

    File f = new File("C:\\Users\\Haroon\\workspace\\ppp\\temperature.txt");
    FileReader file = new FileReader(f);
    BufferedReader buf = new BufferedReader(file);

    String s = null;
    while((s = buf.readLine()) != null){
        System.out.println(s);
    }
}

}

This is strange because the file is in the project's folder. Any help would be appreciated.

4
  • and if you try: "C:/Users/Haroon/workspace/ppp/temperature.txt" are you able to read it? Commented Jul 7, 2012 at 9:43
  • 1
    "This is strange because the file is in the project's folder" - the project's folder is irrelevant when you've specified a fully qualified filename. Commented Jul 7, 2012 at 9:44
  • same error with forward slash. @JonSkeet thanks for that. I know because at first i was writing only temperature.txt instead of the full path. Commented Jul 7, 2012 at 9:47
  • 2
    Double-check your filename. My guess is that there's a typo somewhere. Commented Jul 7, 2012 at 9:48

2 Answers 2

3

That should work. Go to the location of the file, copy the path, paste it in your code, and escape the slashes. You are missing something.

Also check that the name/extension of the file is correct, you could have something like "temperature.txt.txt".

Sign up to request clarification or add additional context in comments.

8 Comments

did what you told. Same problem :(
Ok, next test: copy paste the path from your code in file explorer, remove extra slashes, and hit enter...
I suggest just try simple path first. For example: "c:\\1.txt"
tried both your suggestions. Same problem. Could it be a problem of PATH environment variable
Did you test copy the path to file explorer including the filename? have you tested you program with temperature.txt.txt ?
|
0

I don't know why you are unable to read the file. Its working fine on my system. Since you are making you project in eclipse. I will post a workaround here.

System.out.println(System.getProperty("user.dir"));

Use this command to find the current user directory at the time of execution. Now directly place the file in that user directory. Now you can directly read the file just by its name. For eg:

File f = new File("temperature.txt");

Also as mentioned by 'lxxx' do check the file name and the extension by enabling show file extension option in Windows.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.