Javatpoint Logo

Exception in thread "main" java.io.FileNotFoundException: db.properties (The system cannot find the file spec field)

By: rickys*** On: Thu Nov 14 05:30:19 EST 2013     Question Reputation5 Answer Reputation0 Quiz Belt Series Points0  5Blank User
Ques)1 When i compile the below program i am getting the error.


Progr -

Notepad 1 - I save this file db.properties

user = system
password = oracle

Notepad 2 - I save this file Test.java

import java.util.*;
import java.io.*;

class Test
{
public static void main(String args[]) throws Exception
{
FileReader reader = new FileReader("db.properties");

Properties p = new Properties();
p.load(reader);

System.out.println(p.getProperty("user"));
System.out.println(p.getProperty("password"));
}
}

For compile and run i write at cmd

javac Test.java

then i getting the following error

Exception in thread "main" java.io.FileNotFoundException: db.properties (The system cannot find the file spec
fied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at Test.main(Test.java:12)

what i do

if any jar file load to compile this program then tell me.
Up0Down

 
you have this error because you dont creat file

java.io.FileNotFoundException: db.properties

first step you must creat file by this code, for example:

java.io.File file = new java.io.File("file.txt");

and then you can read from file by this code , for example :

java.util.Scanner input = new java.util.Scanner(file);
Image Created2Down

By: [email protected] On: Thu Nov 14 07:55:55 EST 2013 Question Reputation0 Answer Reputation2 Belt Series Points0 2User Image
Are You Satisfied :9Yes3No
 
Hi Ricky,

The code which is write there is correct and it compile and run properly.

just do one thing both "db.properties file & Test.java" in same folder and try to rum on command form it will run successfully.

Check it.............
Image Created0Down

By: [email protected] On: Thu Nov 14 13:38:17 EST 2013 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :2Yes4No
 
Both files db.properties & Test.java in same folder then

On cmd i write javac Test.java it compile successfully but i write

java Test it generates the exception

then i getting the following error

Exception in thread "main" java.io.FileNotFoundException: db.properties (The system cannot find the file spec
fied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at Test.main(Test.java:12)

tell me what i do
Image Created0Down

By: [email protected] On: Thu Nov 14 20:55:15 EST 2013 Question Reputation5 Answer Reputation0 Belt Series Points0 5User Image
Are You Satisfied :3Yes3No
 
You can go for one more thing. Place the db.properties in a package and import that package.Image Created0Down

By: [email protected] On: Sat Nov 16 04:48:27 EST 2013 Question Reputation0 Answer Reputation359 Belt Series Points0 359User Image
Are You Satisfied :2Yes2No
 
You can mention the full path of properties file

e.g.

FileReader reader=new FileReader("c:\\mylocation\\file.properties");

You should save the properties file by using .properties in double quote. e.g.
save by "db.properties"
Image Created0Down

By: [email protected] On: Tue Nov 19 01:54:56 EST 2013 Question Reputation0 Answer Reputation77 Belt Series Points0 77User Image
Are You Satisfied :2Yes0No