1

I am trying to read from a file using Java. But it shows an error saying that system cannot find the specified file, when the file is lying in the same directory as the source file.

below is the code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class file
{

  public static void main(String[] args) {
    BufferedReader br = null;

    try {

      String sCurrentLine;

      br = new BufferedReader(new FileReader("RoomList.txt"));

      while ((sCurrentLine = br.readLine()) != null) {
        System.out.println(sCurrentLine);
      } 

    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (br != null) br.close();
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }
}

I tried all I could but it just doesn't work. Please let me know the reasons for this. Again I will add that the file RoomList.txt and this code file are in the same directory and even after using fully qualified pathname, it doesn't work. I looked for similar answers for this problem in Stackoverflow and tried them but still it does not seem to be working. Please help.

Tharindu Rusira
  • 691
  • 9
  • 17
Sameeksha Kumari
  • 1,158
  • 2
  • 16
  • 21
  • I have imported these in the beginning. import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; – Sameeksha Kumari Sep 22 '13 at 06:05
  • Can you please post the directory structure ? – Suresh Atta Sep 22 '13 at 06:05
  • specify the full path to the file. Or print the current working directory. It's not the directory of the source file. – Kevin Sep 22 '13 at 06:07
  • See the first half of the top answer here: http://stackoverflow.com/questions/3844307/how-to-read-text-file-from-relative-path-in-a-project – Alexander Sep 22 '13 at 06:09
  • Try moving the file up one level. Eg. If RoomList.txt is in the level1/src directory, move the file to level1 directory and try. – Tharindu Rusira Sep 22 '13 at 06:09
  • C:\semester 2\java\PROJECTS\BookingSystem\RoomList.txt .. this is the text file path. And the path of the source file is C:\semester 2\java\PROJECTS\BookingSystem\file.java – Sameeksha Kumari Sep 22 '13 at 07:09

2 Answers2

0

You should escape the slashes, try changing your code to:

br = new BufferedReader(new FileReader("C:\\semester 2\\java\\PROJECTS\\BookingSystem\\file.java"));
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

There is not problem in your code, its just that when you do not give any file location The System tries to find the file in the Project root.

Check the image below:

enter image description here

EDIT: If you are using Eclipse, it should be like this. WHt dont you just paste a screenshot of your project directory.

C:\CSMAIN\ws\TestFile\src\file.java

C:\CSMAIN\ws\TestFile\RoomList.txt

If you are not using any IDE then, you can create a project and put both the files in the project directory and compile it using command prompt and run it. It works.

dharam
  • 7,882
  • 15
  • 65
  • 93