Being new to java am trying to understand the terminology and works around...

If i have a simple text file userdetails.txt which contains users name, password, home_dir_context (this is just for questioning).. Which function between the two do i use to get the path where this is located : c:/Java Projects/Users/Details/userdetails.txt? And how do i call this function. Could you also explain the difference by stating why should i use the one over the other? I understand that getResourceAsStream returns an instream while getResource returns URL (which am not sure whether a URL is a path including c:/ or what)?

Lastly, is it preferably to have my text file as userdetails.properties, if so why? Will stop here for now

Thanks in advance

ello
me,i prefer using the .properties files because you can keep connection settings in them ,for example, and then use java code to get them. This way if the settings will change you will only have to change them in there(the .config file) and not going through a lot of java code finding them,and making mistakes.

a config.properties file should look like this:

server = localhost
port = 21
username = bla
password = bla

and then you request them in the code

Properties p = new Properties();
			InputStream is;
			try {
				is = new FileInputStream("config.properties");
				p.load(is);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

//and then
String server = p.getProperty("server");
String port = p.getProperty("port");
//and so on

ello
me,i prefer using the .properties files because you can keep connection settings in them ,for example, and then use java code to get them. This way if the settings will change you will only have to change them in there(the .config file) and not going through a lot of java code finding them,and making mistakes.

a config.properties file should look like this:

server = localhost
port = 21
username = bla
password = bla

and then you request them in the code

Properties p = new Properties();
			InputStream is;
			try {
				is = new FileInputStream("config.properties");
				p.load(is);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

//and then
String server = p.getProperty("server");
String port = p.getProperty("port");
//and so on

@linu, i see you not using paths to retrieve you config.properties file... If needed to, which function would you be using (getResourceAsStream or getResource)? How does FileInputStream know where to get the file?

This article has been dead for over six months. Start a new discussion instead.