0

I have a file named config.properties in Eclipse with the following content:

PATH_TO_A_FILE=a.txt PATH_TO_B_FILE=b.txt PATH_TO_C_FILE=c.txt 

In my code, I need to use these properties like this:

conf.put("PATH_TO_B_FILE", properties.getProperty("PATH_TO_B_FILE")); 

which files A.txt and B.txt are in the same path of the config.properties in the workspace folder

What I should do read those paths from my config.properties file?

Also: should I add a path before b.txt? Should I write it as /home/user/workspace/b.txt or .home.user.Dersktop.b.txt?

2
  • 1
    If you put relative path (b.txt, ../b.txt, src/main/resources/b.txt), it will take relative path. If you put full path(/usr/b.txt, /root/b.txt, /b.txt), it will take full path. So, it's depend on your settings. But, .home.user.Dersktop is not valid path, because, it's (maybe) internal path-resolver of Eclipse which is not appliable for Java Commented Dec 21, 2015 at 16:21
  • thanks for your reply , i solved it Commented Dec 21, 2015 at 16:23

1 Answer 1

1

Suppose that you have a file with path /home/user/Desktop/my.conf that contains properties, then you can load these properties in a Properties object like this:

Properties properties = new Properties(); InputStream input = new FileInputStream("/home/user/Desktop/my.conf"); properties.load(input); 

Now you can get the properties like this:

String pathToA = properies.get("PATH_TO_A_FILE"); 

Obviously, you shouldn't put that properties file my.conf on your desktop. Are you creating a web app? In that case, you can ship the properties file with your jar. Files can be read from a jar using an InputStream, but that's a different question ;-)

Whether or not you should add a full path in the properties file also depends on the context of your application. In a server environment, you may not have a /home/user/Desktop directory. If you want an answer to that second question, you should clarify the context of your question. For instance: is your application a desktop or server application? what is the working directory of your application?

Sign up to request clarification or add additional context in comments.

6 Comments

I wouldn't do that. I would use the relative path to the file. Imagine this: you put your web application in a jar and you deploy it on a server. The application server where your jar is deployed probably won't have access to /home/user/workspace/myproject/a.txt. It is much better to put a.txt in your jar and load the file from the jar.
I don't understand the additional question. Did you try anything? If so, did it work? What kind of answer do you expect? Seems like you're wasting time on a non-issue...
OK, so you create the files in the working directory. In that case, why wouldn't you use relative paths. I really don't see the issue. Maybe there is no issue. Maybe you just haven't tried anything yet...
English is not my tonuge language i'm trying to write with it !! i think too it's not very bad as you wrote so be careful not to taunt anyone ?? i think i post my problem but you cannot understand it ! so thanks for your time and if you coudln't help or understand me or complete your answer ... don't reply
You want to read a path from a configuration file. That's very easy to achieve. However: you seem to have a problem. Otherwise you wouldn't post a question. Unfortunately, you don't succeed in explaining the problem. The fact that nobody but me tries to help you should be an indication that you should do a better effort explaining what goes wrong. Does anything go wrong? Please explain!
|