Results 1 to 6 of 6

Thread: Reading properties of a file

  1. #1
    Join Date
    Dec 2009
    Posts
    263

    Reading properties of a file

    Hello,
    I am trying to da a program in java , which deals with property of a file. I have a property file of this type
    Code:
    par1 = name
    par2 = first_name
    Is there a simple way to retrieve parameter 1 and 2 in a java program? I have not found one. Any help in this will be highly appreciated. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    335

    Re: Reading properties of a file

    Hello,
    I have a code, just have a look at it
    Code:
    Public class conf {
      private static Final String confg = "config"+ File.separator+"config.properties";
      Public static prresbnd getConfig(ServletContext context) {
         String pth = context.getRealPath(ConfigFile);
         return new prresbnd(new FileInputStrem(pth));
      }
    }
    And also have a look at this
    Code:
    prresbnd p = GestionnaireCongif.getConfig(context);
    String par1 = p.getString("pat1");

  3. #3
    Join Date
    Nov 2009
    Posts
    446

    Re: Reading properties of a file

    Hello,
    I have a small program, i think you will be interested in this

    Code:
    ResourceBundle bnd = ResourceBundle.getBundle ( "filename");
    try (
    String key = "pr1" / / for example
    bnd.getString return (key);
    ) Catch (MissingResourceException e) (
    return key;
    }
    See, if this example helps you.

  4. #4
    Join Date
    Nov 2009
    Posts
    333

    Re: Reading properties of a file

    Hello,
    There may be more simple than using the ResourceBundle. Have a look at the following code

    Code:
    FileInputStream f;
    Properties p;
    
    try
    {
    f = new FileInputStream ("test.properties");
    p = new Properties ();
    p.load (f);
    }

  5. #5
    Join Date
    Nov 2009
    Posts
    356

    Re: Reading properties of a file

    Hello,
    After all the working is finished please do not forget to close the stream

    Code:
    	Properties p = new Properties();
    		try {
    			FileInputStream st = new FileInputStream("test.properties");
    			try {
    				p.load(st);
    			} finally {
    				st.close();
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}

  6. #6
    Join Date
    Nov 2009
    Posts
    343

    Re: Reading properties of a file

    Hello,
    Just view the following sample code, it shows the basic of how the properties of the file are displayed
    Code:
    Properties pr = new Properties();
    
        try {
            pr.load(new FileInputStream("filename.prerties"));
        } catch (IOException e) {
        }
        
        // Write prerties file.
        try {
            pr.store(new FileOutputStream("filename.prerties"), null);
        } catch (IOException e) {
        }

Similar Threads

  1. Properties file in java
    By HardWeaR in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 01:54 AM
  2. Properties of .ini file
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 25-01-2010, 01:43 PM
  3. Cannot View AVI file Properties
    By Balgovind in forum Windows Software
    Replies: 2
    Last Post: 25-05-2009, 12:48 PM
  4. Change properties of PDF/A file
    By !const in forum Windows Software
    Replies: 3
    Last Post: 04-05-2009, 02:32 PM
  5. C#.net how to access properties of a file?
    By Henrietta in forum Software Development
    Replies: 1
    Last Post: 11-08-2008, 01:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,569,803.07896 seconds with 17 queries