Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Structured Properties File?

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Structured Properties File?

    Hi all,

    Im new here and new to java so please be nice

    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.


  2. #2
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?
    Last edited by moka; October 18th, 2010 at 07:03 PM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.
    Map<String, Map<String,String> properties = new HashMap<String,Map<String,String>>();

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by moka; October 18th, 2010 at 07:32 PM.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Structured Properties File?

    Quote Originally Posted by moka View Post
    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.

  6. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  7. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...?

    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 racle
    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

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

Similar Threads

  1. Replies: 10
    Last Post: January 12th, 2011, 05:48 AM
  2. insert(embed) a file object (.txt file) in MS excel sheet using java.
    By jyoti.dce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 12th, 2010, 08:16 AM
  3. Accessing Properties File
    By java_mein in forum Java Servlet
    Replies: 5
    Last Post: May 14th, 2010, 02:44 AM
  4. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 09:11 AM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM