0

I am trying to read a file from the hard disk in java. However when I am trying to: BufferedReader inputReader = new BufferedReader(new FileReader("weather.txt")); and I'm receiving the following message:

Exception in thread "main" java.io.FileNotFoundException: C:\weather.txt (System cannot define the specific file) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.(FileInputStream.java:138) at java.io.FileInputStream.(FileInputStream.java:93) at java.io.FileReader.(FileReader.java:58)

I tried to change the location of the file many times. However I got the same error. The location of the file is C:\weather.txt. What I am missing here?

EDIT: Based on the proposed answer I tried to do the following:

File targetFile = new File("C:\\", "weather.txt");
FileReader fr = new FileReader(targetFile);//BufferedReader datafile = readDataFile("C://weather.txt");
BufferedReader br  = new BufferedReader(fr);

It seems that I cant use at all bufferedReader. What is happening here?

Jose Ramon
  • 5,572
  • 25
  • 76
  • 152

2 Answers2

1

Try to use this:

  File targetFile = new File(targetPath, filename);

Or you can create a folder C:\\folder\\weather.txt somewhere else.

Abdelhak
  • 8,299
  • 4
  • 22
  • 36
  • However my code is inside a function which returns a bufferedReader how can I return instead a file? What is the relationship between bufferedReader and File? – Jose Ramon Dec 31 '15 at 14:38
  • `new BufferedReader(new FileReader(targetFile));` will do the work – alias_boubou Dec 31 '15 at 14:40
1

The error as stated by you is completely depends on java.io.FileNotFoundException exception. This exception occurred due to try to open a file which not exist in the given path. As you are trying to get a BufferedReader out of the file name, you better need to check of the file exist or not, before opening it.

Possible remedy

public static BufferedReader getFileBufferedReader(String fileName) throws FileNotFoundException, IOException{
    File file = new File(fileName);
    if (!file.exists()) {  
        OutputStream out = new FileOutputStream(file);
        out.close();
    }
    FileReader fr = new FileReader(file);
    return new BufferedReader(fr);
}

Idea

First confirm if the given fileName exist or not by using file.exists() method and then if file not exist, create one file with the name as given in fileName. As soon as there is an existing file your method will work and you can return a BufferedReader out of fileName.

  • Yup I got it. file.exists doesnot seem to find the file. However the file is the provided location? Is there a way that I got issues due to admin rights? – Jose Ramon Dec 31 '15 at 15:56
  • To get **admin** rights, check this out, [http://stackoverflow.com/questions/1385866/java-run-as-administrator] – user3539722 Dec 31 '15 at 16:04
  • I found it, it seems that the file was stored weather.txt.txt. :( – Jose Ramon Dec 31 '15 at 16:04