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.

Community
  • 1
  • 1
stud91
  • 1,854
  • 6
  • 31
  • 56
  • and if you try: "C:/Users/Haroon/workspace/ppp/temperature.txt" are you able to read it? – Francisco Spaeth Jul 07 '12 at 09: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. – Jon Skeet Jul 07 '12 at 09: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. – stud91 Jul 07 '12 at 09:47
  • 2
    Double-check your filename. My guess is that there's a typo somewhere. – Jon Skeet Jul 07 '12 at 09:48

2 Answers2

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

User
  • 31,811
  • 40
  • 131
  • 232
  • did what you told. Same problem :( – stud91 Jul 07 '12 at 09:52
  • Ok, next test: copy paste the path from your code in file explorer, remove extra slashes, and hit enter... – User Jul 07 '12 at 09:54
  • I suggest just try simple path first. For example: "c:\\1.txt" – Aleksandr Kravets Jul 07 '12 at 09:57
  • tried both your suggestions. Same problem. Could it be a problem of PATH environment variable – stud91 Jul 07 '12 at 10:02
  • No, it's not related with the PATH. If you can't open the file when copy from your code to the file explorer, then you are near to the cause. Take this path, put it in a text editor, compare it with the correct one... – User Jul 07 '12 at 10:06
  • I can open the file from this. I have taken a screen shot of everything. here it is: http://min.us/mG4uLTLBX – stud91 Jul 07 '12 at 10:09
  • 2
    Did you test copy the path to file explorer *including* the filename? have you tested you program with temperature.txt.txt ? – User Jul 07 '12 at 10:18
  • Also try removing the .txt from the filename, if it's still displayed as text file, then you had the extension in the name and that's wrong. – User Jul 07 '12 at 10:20
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.

dejavu
  • 3,236
  • 7
  • 35
  • 60