Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Reading properties of a file

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 14-02-2010
Member
 
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.

Reply With Quote
  #2  
Old 14-02-2010
Member
 
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");
Reply With Quote
  #3  
Old 14-02-2010
Member
 
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.
Reply With Quote
  #4  
Old 14-02-2010
Member
 
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);
}
Reply With Quote
  #5  
Old 14-02-2010
Member
 
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();
		}
Reply With Quote
  #6  
Old 14-02-2010
Member
 
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) {
    }
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 01:53 PM.