0

I am using Eclipse IDE to develop a java program that reads a text file. I am using FileReader class to read a text file in Java. It works fine, if I place the text file inside the src folder of the project, but when I place it in some other location, I get a FileNotFoundException. I have no idea what must be wrong with it. Some help will be much appreciated.

Mike Laren
  • 8,028
  • 17
  • 51
  • 70
thisisppn
  • 37
  • 6
  • 1
    Most likely, you haven't provided the complete (and correct) path to the file outside of your eclipse workspace – Stultuske Apr 02 '15 at 11:27
  • Whenever compiler does not find a path to your file it gives you `FileNotFoundException` you need to change your path as well accordingly when you move your file. – Parth Soni Apr 02 '15 at 11:29
  • src folder is part of ClassPath and files there would be read directly. If you place it some where give full correct path to the file. – vikeng21 Apr 02 '15 at 11:29
  • @Stultuske I did profide the complete and correct path to the file outside the workspace ... but, still the same error. – thisisppn Apr 02 '15 at 11:30
  • can u please provide the code ? – Vivek Singh Apr 02 '15 at 11:31
  • @vikeng21 ... Suppose i placed the file in E: inside a folder called Test ... Would this be the correct path address?? "E:/Test/test.txt" If so, then I have tried this out. But, no help. – thisisppn Apr 02 '15 at 11:32
  • String file = "C:/Users/ADMIN/JavaWorkShop/TextToDB/src/test.txt"; FileReader f = new FileReader(file); br = new BufferedReader(f); while ((thisLine = br.readLine()) != null) { String[] input = thisLine.split(" "); st.executeUpdate("insert into test values("+Integer.parseInt(input[0])+", '"+input[1]+"', '"+input[2]+"')"); count++; } System.out.println(count+" rows inserted"); here's the partial code, I am actually trying to read the text file and insert the data into database using JDBC. This is the File reading part. – thisisppn Apr 02 '15 at 11:35
  • possible duplicate of [Getting FileNotFoundException](http://stackoverflow.com/questions/23182968/getting-filenotfoundexception) – durron597 Apr 23 '15 at 15:21

2 Answers2

0

File reader is trying to read the file from classpath by default, that's why it doesn't fail when you place the file inside the classpath (src) path. When you place the file outside of the src folder, you have to give full path to read the file. Example : File f=new File("\home\user\xxx\file.txt");

Ozgen
  • 1,072
  • 10
  • 19
  • is it because i am using FileReader that i am getting this error?? Let me try using the File class. Thank You. – thisisppn Apr 02 '15 at 11:36
  • FileReader or File, it doesn't matter which one you are using, file name resolution is the same. Give full path of the file if it is not in the classpath – Ozgen Apr 02 '15 at 11:38
  • Its working now, i think it was not working because, I did not use File f = new File(path) ... before using the FileReader ... But, Now i tried it out with the FIle statement, and its working just fine. Thank you so much. Big Help – thisisppn Apr 02 '15 at 11:44
-1

You should create File object like this:

File f = new File("D://new folder//file.txt");

and take this object to File reader

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89