Structured Properties File?
Hi all,
Im new here and new to java so please be nice :rolleyes:
Im writing a program that will run in a command line linux envrioment and i need to gather information from the user regarding multiple ftp accounts. To gather this information i want to go down a simple properties file route where by the file would look something like this...
# ftp1
ACCOUNT=IBM
PASSWORD=letmein
IP=192.168.0.1
# ftp2
ACCOUNT=MICROSOFT
PASSWORD=letmein
IP=192.168.0.2
# ftp3
ACCOUNT=APPLE
PASSWORD=letmein
IP=192.168.0.3
But i have run into the problem whereby the properties class does not allow sectioning and will only accept "1 key, 1 value".
Can anyone suggest what to do? Are there any other classes to use that will allow sectioning? Or is there another way to gather the information from the user (simple please as im very new).
The user would have to edit the file themselves so it needs to look simple...
Please help.
Re: Structured Properties File?
There maybe up to 30-40 different FTP accounts that will probably have 3-4 more attributes so someone has recommened i use a database... Does anyone recommend a SMALL and easy to use database that doesnt require the user to install anything extra?
Re: Structured Properties File?
Along the lines of your second post, linux may have mysql already installed (check the /usr/bin for the binaries). If it is there it is probably not started so you'd have to start the server then create your database. If you don't know sql you are opening a can of worms, and it isn't as portable as a simple text file which can be read from your application on multiple systems without installing 3rd party database software (if this is an issue to consider).
Along the lines of your first post, you can create nested HashMap to store the properties read from the file. It doesn't have the io utilities that the Properties class contains, but will suffice for locating specific key/value entries.
Code java:
Map<String, Map<String,String> properties = new HashMap<String,Map<String,String>>();
Re: Structured Properties File?
Thank you for your reply.
Can i ask you, from an experienced viewpoint, what you would go for? I know an OK amount about sql and could probably get by if it was simple enough...
This is for my final year project and need to make sure i make the right choice early on in the development.
The project idea is to create an automated ftp service that gathers information on the clients (using a properties file or a small db) and used that to schedule FTP send and receive's.
Re: Structured Properties File?
Quote:
Originally Posted by
moka
Can i ask you, from an experienced viewpoint, what you would go for?
This is a difficult question to answer, and depends upon the requirements. Like I said above, a file could make the code more portable from one system to the next in that there wouldn't be a 3rd party database requirement. So if a requirement is to install this or a derivative of this on multiple systems then going this way would not rely on also setting up a database and transferring the data to said systems. However a database allows you to store a lot of relational information, so if there is more information on top of this that you could or want to store, and the storage can be made more efficient and dynamic by relating the data through different tables, then you could accomplish a lot by having your application backed by a database.
Re: Structured Properties File?
Hi there,
Due to the fact i think a database would be overkill for the information im edging more towards a nested hashmap. I have never used one before though is it possible for you to point me in the right direction in how to import a properties file laid out simular to the one about and store it into a nested hashmap?
Re: Structured Properties File?
Ive got this far so far in my experimentation... But instead of specifiying the hashmap in the code i want to pull it from a properties file...?
Code :
import java.util.HashMap;
public class nested {
public static void main(String args[]) {
HashMap<String, HashMap<String, String>> map = new HashMap<String, HashMap<String,String>>();
map.put("ftp1", new HashMap<String, String>());
map.put("ftp2", new HashMap<String, String>());
map.put("ftp3", new HashMap<String, String>());
map.put("ftp4", new HashMap<String, String>());
map.get("ftp1").put("account", "ibm");
map.get("ftp1").put("ip", "10.0.0.1");
map.get("ftp1").put("password", "letmein");
map.get("ftp1").put("dir", "C:tester");
map.get("ftp1").put("direction", "send");
map.get("ftp2").put("account", "microsoft");
map.get("ftp2").put("ip", "10.0.0.2");
map.get("ftp2").put("password", "letmein");
map.get("ftp2").put("dir", "C:tester");
map.get("ftp2").put("direction", "receive");
map.get("ftp3").put("account", "oracle");
map.get("ftp3").put("ip", "10.0.0.6");
map.get("ftp3").put("password", "letmein");
map.get("ftp3").put("dir", "C:tester");
map.get("ftp3").put("direction", "both");
map.get("ftp4").put("account", "java");
map.get("ftp4").put("ip", "10.0.0.9");
map.get("ftp4").put("password", "letmein");
map.get("ftp4").put("dir", "C:tester");
map.get("ftp4").put("direction", "receive");
int sizeOfMap=map.size();
for(int counter=0; counter<=sizeOfMap; counter++)
if(map.containsKey("ftp"+counter)){
String valueFromMap = map.get("ftp"+counter).get("account");
System.out.println("Account :"+valueFromMap);
valueFromMap = map.get("ftp"+counter).get("ip");
System.out.println("IP :"+valueFromMap);
valueFromMap = map.get("ftp"+counter).get("password");
System.out.println("Password :"+valueFromMap);
valueFromMap = map.get("ftp"+counter).get("dir");
System.out.println("Directory :"+valueFromMap);
valueFromMap = map.get("ftp"+counter).get("direction");
System.out.println("Direction :"+valueFromMap);
System.out.println("");
This gives me this output
Account :ibm
IP :10.0.0.1
Password :letmein
Directory :C:tester
Direction :send
Account :microsoft
IP :10.0.0.2
Password :letmein
Directory :C:tester
Direction :receive
Account :oracle
IP :10.0.0.6
Password :letmein
Directory :C:tester
Direction :both
Account :java
IP :10.0.0.9
Password :letmein
Directory :C:tester
Direction :receive
Re: Structured Properties File?
You could just parse the file line by line as you read them in, or use Regular Expressions (The Java™ Tutorials > Essential Classes) which could help you loop over the file contents to pull out each entry and the associated key/value pairs