Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read a file properties 1

Status
Not open for further replies.

tyris

Programmer
Nov 2, 2000
311
FR
hi all,

i do have a java class, it's a WebService in fact, coded in java. I must add something, and as i learned by myself java there some things i don't know.

my class must access to a properties file to store data like server, dbs path etc... this file will also be changeable by an administrator on the machine

i'have seen that with java.util.Properties there is a method to save the properties into an outputstream (consequently into a file i guess), but i have never dealed with output and files, so i'm a bit lost, could someone give me some clues, or a link to some code ?
Best regards,
Elise, XML learning girl X-)
 
Hi,
This class should take care of ur problems..its a pretty long one though.. but takes care of saving the properties object and everything..

Hope this helps,
Reddy316.

import java.util.Enumeration;
import java.util.Properties;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;


/**
* Allows resource bundling
*/
public class Settings
{

//The single instance
private static Settings instance;

//The filename of the property file
private String strFileName;
private Properties oProperties;


/**
* Constructs a Settings
* @exception IOException when the file may not be present
* @param String the file name
*/
private Settings(String strFileName) throws IOException
{
oProperties = new Properties();
setFilename(strFileName);
}


/**
* method to get the single instance of this class
* @return the single instance of this class based on the file name specified.
* @param the name of the config file
* @exception IllegalStateException thrown when the single instance has not
* been created via a call to setInstance().
*/
public static Settings getInstance(String strFileName)
{
String strPropfile = "";
if (instance == null)
{
strPropfile = strFileName;
try
{
instance = new Settings(strPropfile);
}
catch (Exception e)
{
throw new IllegalStateException("Failed to load property settings.");
}

}

return instance;
}


/**
* Sets the property file used by the single instance.
* @param String the config file name to be picked up
* @exception IllegalArgumentException thrown when the filename is null.
*/
public void setFilename(String strFileName) throws IOException
{
if (strFileName == null)
{
throw new IllegalArgumentException("filename cannot be null");
}
else if (!strFileName.equals(this.strFileName))
{
if (this.strFileName != null)
{
save(); //save current property file before replacing
}
this.strFileName = strFileName;
load(strFileName);
}
}


/**
* method to get the current config file associated with the instance
* @return String the filename of the property file used by the single instance.
*/
public String getFilename()
{
return strFileName;
}


/**
* method to load the current config file associated with the instance
* @param the filename of the property file used by the single instance.
*/
public void load(String strFileName) throws IOException
{
oProperties.load(new FileInputStream(strFileName));
}


/**
* method to save the new property values back to the property file.
*/
public void save()
{
if (strFileName == null)
return;
try
{
FileOutputStream fos = new FileOutputStream(strFileName);
oProperties.store(fos,strFileName);
}
catch (IOException e)
{
System.out.println("Failed to save properties");
}
}


/**
* method to get the properties file associated with the instance of this class
* @return the Properties object used by Settings
*/
public Properties getProperties()
{
return oProperties;
}


/**
* method to check if the key exists in the property file
* @return boolean true if key is found, false if the key is not present
*/
public boolean containsKey(String key)
{
return oProperties.containsKey(key);
}


/**
* method to return the value present in the property file for the given key
* @param String the key for which the value is to be found
* @return String the property value given its key,returns null if the key is not found.
*/
public String getProperty(String key)
{
return oProperties.getProperty(key);
}


/**
* method to return the value present in the property file for the given key
* @return String the property value given its key, returns defaultValue if the key
* is not found.
*/
public String getProperty(String key, String defaultValue)
{
return oProperties.getProperty(key, defaultValue);
}


/**
* method to get the keys in the property file as enumeration
* @return Enumeration an enumeration of the property keys/names.
*/
public Enumeration propertyNames()
{
return oProperties.propertyNames();
}


/**
* method to set the key value pair in the property file
* @param String the key
* @param String the value to be associated with the key
*/
public void setProperty(String key, String value)
{
oProperties.setProperty(key, value);
}

}
 
thanks ! i gonna test and adapt it, i will let you know if there's something wrong Best regards,
Elise, XML learning girl X-)
 
GREAT that's what i was looking for.

I do have a new question :
what possibilities do we have to encypt an information in the properties file ? Best regards,
Elise, XML learning girl X-)
 
Hi,

Maybe one of the easiest way is to use BASE64Encoder/Decoder (comes with Sun's SDK):

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

public class B64Test
{
public static void main(String[] argv) throws Exception
{
// Encoding....
BASE64Encoder encoder = new BASE64Encoder();
String value = "something...";
String encoded = encoder.encode(value.getBytes());
System.out.println("Encoded: " + encoded);

// ...and decoding again
BASE64Decoder decoder = new BASE64Decoder();
String decoded = new String(decoder.decodeBuffer(encoded));
System.out.println("Decoded: " + decoded);
}
}

-Vepo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top