struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From micael <caraun...@harbornet.com>
Subject RE: Simple Question
Date Mon, 09 Sep 2002 19:57:46 GMT
Just write a helper class that opens the file, loads it into a properties 
object, reads the properties, and closes the file.  Something like the 
following:

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

public final class SilvaProperties {

     public SilvaProperties setProperty(String file, String key, String 
value) {
                                  key  = Replace.replace(key, " ", "");
         File                   hold  = new File(file);
         Properties  properties = new Properties();
         try {
             FileInputStream input = new FileInputStream(hold);
             properties.load(input);
             properties.setProperty(key, value);
             FileOutputStream output= new FileOutputStream(hold);
             properties.store(output, " -- TRESBEAU -- ");
             output.close();
             input.close();
         } catch (FileNotFoundException fnfe) {
         } catch (IOException ioe) {
         }
         return this;
     }

     public String getProperty(String file, String key) {
                                  key  = Replace.replace(key, " ", "");
         File                   hold  = new File(file);
         Properties  properties = new Properties();
         try {
             FileInputStream input = new FileInputStream(hold);
             properties.load(input);
             input.close();
         } catch (FileNotFoundException fnfe) {
         } catch (IOException ioe) {
         }
         return properties.getProperty(key);
     }

     public Properties getProperties(String file) {
         Properties   properties = new Properties();
         File            hold       = new File(file);
         try {
             FileInputStream input  = new FileInputStream(hold);
             properties.load(input);
             input.close();
         } catch (FileNotFoundException fnfe) {
         } catch (IOException ioe) {
         }
         return properties;
     }


         public final class Replace {
                 public synchronized static String replace(String content, 
String before, String after) {
                                 int position = content.indexOf(before);
                                 while (position > -1) {
                                 content  = content.substring(0, position) 
+ after + content.substring(position + before.length());
                                 position = content.indexOf(before, 
position + after.length());
                                 }
                         return content;
                 }
         }
}
}

At 03:21 PM 9/9/2002 -0400, you wrote:
>Are doing this within an Action?
>
>
>James Mitchell
>Software Engineer\Struts Evangelist
>Struts-Atlanta, the "Open Minded Developer Network"
>http://www.open-tools.org/struts-atlanta
>
>
>
>
> > -----Original Message-----
> > From: Mark Silva [mailto:msilva@authenex.com]
> > Sent: Monday, September 09, 2002 3:07 PM
> > To: Struts Users Mailing List
> > Subject: Simple Question
> >
> >
> > This is probably a simple question, but i cannot find the answer
> > in any straighforward place.
> >
> > how do i get a property from my ApplicationResources.properties
> > file within some java code.  i know how to do this using a bean
> > tag, but not otherwise.  is there a helper class to do this?
> >
> > i am using this to assign default values for a form (within a
> > form object).
> >
> > thanks,
> > mark
> >
> > --
> > To unsubscribe, e-mail:
> > <mailto:struts-user-unsubscribe@jakarta.apache.org>
> > For additional commands, e-mail:
> > <mailto:struts-user-help@jakarta.apache.org>
> >
> >
>
>--
>To unsubscribe, e-mail:   <mailto:struts-user-unsubscribe@jakarta.apache.org>
>For additional commands, e-mail: <mailto:struts-user-help@jakarta.apache.org>



--
To unsubscribe, e-mail:   <mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-user-help@jakarta.apache.org>


Mime
View raw message