You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Daniel Chacón Sánchez <da...@gmail.com> on 2007/03/01 17:23:02 UTC
OutputStreamPath
Hi all
I have a question, how I can set the path for the OutputStream like i get it
for the InputStream with getResourceAsStream?
This is my code:
Properties properties = new Properties();
String pathToConfigurationFile =
"common/properties/Configuracion.properties";
InputStream input =
Thread.currentThread
().getContextClassLoader().getResourceAsStream(pathToConfigurationFile);
//Load the properties
properties.load(input);
input.close();
//Set the property datasource
properties.setProperty("datasource", datasource);
//Now I want to save the changes to the properties file
OutputStream out= new FileOutputStream(
pathToConfigurationFile);
properties.store(out, "");
//But an exception occurs and it tells me that the
"common/properties/Configuracion.properties" was not found.
How I can get the correct path for the outputStream like i did with the
inputStream
Thanks!!
Re: OutputStreamPath
Posted by Daniel Chacón Sánchez <da...@gmail.com>.
Got it:
String path= Thread.currentThread().getContextClassLoader().
getResource(pathToConfigurationFile).getPath();
OutputStream out = new FileOutputStream(path);
properties.store(out, "");
2007/3/1, Daniel Chacón Sánchez <da...@gmail.com>:
>
> Someone?
>
> Hi all
>
> I have a question, how I can set the path for the OutputStream like i get
> it for the InputStream with getResourceAsStream?
>
> This is my code:
>
>
> Properties properties = new Properties();
> String pathToConfigurationFile =
> "common/properties/Configuracion.properties";
> InputStream input =
> Thread.currentThread().getContextClassLoader().getResourceAsStream(
> pathToConfigurationFile);
>
> //Load the properties
> properties.load(input);
> input.close ();
>
> //Set the property datasource
> properties.setProperty("datasource", datasource);
>
> //Now I want to save the changes to the properties file
>
> OutputStream out= new FileOutputStream(
> pathToConfigurationFile);
> properties.store(out, "");
>
> //But an exception occurs and it tells me that the
> "common/properties/Configuracion.properties" was not found.
>
> How I can get the correct path for the outputStream like i did with the
> inputStream
>
> Thanks!!
>
Re: OutputStreamPath
Posted by Daniel Chacón Sánchez <da...@gmail.com>.
Yes you are right, but what i have in the properties file is the database to
which the user wants to connect, for example oracle or sql.
Thanks
2007/3/1, Laurie Harper <la...@holoweb.net>:
>
> Dave Newton wrote:
> > --- Daniel Chacón Sánchez <da...@gmail.com>
> >> I have a question, how I can set the path for the
> >> OutputStream like i get it for the InputStream with
> >> getResourceAsStream?
> >
> > I'm not sure you can do that, particularly since a
> > resource might be inside a JAR file or be located in a
> > place you do not have write acess to.
>
> Yup, there's no guarantee that the contents of a webapp can be written
> to in all deployment contexts. However, as long as you know you will be
> deploying it to a container that allows the file to be written to, you
> should be OK.
>
> > If you know that the properties file is in a specific
> > place and that it makes sense to write to it then I
> > would think you'd use typical file i/o and determine
> > the path via getRealPath.
>
> In your case, a simple change to the code you've already written should
> do the trick. Something like this (untested):
>
> URL res = Thread.currentThread()
> .getContextClassLoader()
> .getResource(pathToConfigurationFile);
>
> InputStream input = new FileInputStream(res);
>
> //Load the properties
> properties.load(input);
> input.close();
>
> ...
>
> // Save the properties
> OutputStream out = new FileOutputStream(res);
> properties.store(out, "");
>
> > It confuses me why you'd want to do this in a webapp,
> > though. Settings you are likely to modify from within
> > the app should be in a database, not a properties file
> > (IMO, anyway).
>
> Agreed. Or otherwise externalized, for example through JNDI or something.
>
> L.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
Re: OutputStreamPath
Posted by Daniel Chacón Sánchez <da...@gmail.com>.
I just see your answer, thanks!!! In fact is the same that the solution i
Found, Thanls!!!!
2007/3/1, Laurie Harper <la...@holoweb.net>:
>
> Dave Newton wrote:
> > --- Daniel Chacón Sánchez <da...@gmail.com>
> >> I have a question, how I can set the path for the
> >> OutputStream like i get it for the InputStream with
> >> getResourceAsStream?
> >
> > I'm not sure you can do that, particularly since a
> > resource might be inside a JAR file or be located in a
> > place you do not have write acess to.
>
> Yup, there's no guarantee that the contents of a webapp can be written
> to in all deployment contexts. However, as long as you know you will be
> deploying it to a container that allows the file to be written to, you
> should be OK.
>
> > If you know that the properties file is in a specific
> > place and that it makes sense to write to it then I
> > would think you'd use typical file i/o and determine
> > the path via getRealPath.
>
> In your case, a simple change to the code you've already written should
> do the trick. Something like this (untested):
>
> URL res = Thread.currentThread()
> .getContextClassLoader()
> .getResource(pathToConfigurationFile);
>
> InputStream input = new FileInputStream(res);
>
> //Load the properties
> properties.load(input);
> input.close();
>
> ...
>
> // Save the properties
> OutputStream out = new FileOutputStream(res);
> properties.store(out, "");
>
> > It confuses me why you'd want to do this in a webapp,
> > though. Settings you are likely to modify from within
> > the app should be in a database, not a properties file
> > (IMO, anyway).
>
> Agreed. Or otherwise externalized, for example through JNDI or something.
>
> L.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
Re: OutputStreamPath
Posted by Laurie Harper <la...@holoweb.net>.
Christopher Schultz wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Dave,
>
> Dave Newton wrote:
>> --- Laurie Harper <la...@holoweb.net> wrote:
>>> URL res = Thread.currentThread()
>>> .getContextClassLoader()
>>> .getResource(pathToConfigurationFile);
>>> // Save the properties
>>> OutputStream out = new FileOutputStream(res);
>>> properties.store(out, "");
>> Ooo, I didn't know that.
>>
>> Hopefully I'll never want to use it, but still really
>> good to know! :)
>
> I'm not sure where this will work. FileOutputStream doesn't accept a URL
> as a constructor argument. I think it's something more involved like:
>
> URL res = ...;
> URLConnection conn = res.openConnection();
> conn.setDoOutput(true);
> OutputStream out = conn.getOutputStream();
>
> ....
>
> Of course, you'd want lots of error checking, too.
>
> It's still butt-ugly.
Or, more simply,
URL res = ...;
String path = res.getPath();
OutputStrem out = new FileOutputStream(path);
...
I think you should be guaranteed a file URL from getResource(), at least
in the situations where this whole approach is viable (i.e. where the
target file is going to be a writable resource).
L.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: OutputStreamPath
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Dave,
Dave Newton wrote:
> --- Laurie Harper <la...@holoweb.net> wrote:
>> URL res = Thread.currentThread()
>> .getContextClassLoader()
>> .getResource(pathToConfigurationFile);
>> // Save the properties
>> OutputStream out = new FileOutputStream(res);
>> properties.store(out, "");
>
> Ooo, I didn't know that.
>
> Hopefully I'll never want to use it, but still really
> good to know! :)
I'm not sure where this will work. FileOutputStream doesn't accept a URL
as a constructor argument. I think it's something more involved like:
URL res = ...;
URLConnection conn = res.openConnection();
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
...
Of course, you'd want lots of error checking, too.
It's still butt-ugly.
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFF52Oh9CaO5/Lv0PARAo7RAJ9PchB6c8AOOaFF0KnpxX64oQLuUgCgn6MU
qSffOgmCivXDNQOWS+0te/g=
=Lxay
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: OutputStreamPath
Posted by Dave Newton <ne...@yahoo.com>.
--- Laurie Harper <la...@holoweb.net> wrote:
> URL res = Thread.currentThread()
> .getContextClassLoader()
> .getResource(pathToConfigurationFile);
> // Save the properties
> OutputStream out = new FileOutputStream(res);
> properties.store(out, "");
Ooo, I didn't know that.
Hopefully I'll never want to use it, but still really
good to know! :)
d.
____________________________________________________________________________________
Food fight? Enjoy some healthy debate
in the Yahoo! Answers Food & Drink Q&A.
http://answers.yahoo.com/dir/?link=list&sid=396545367
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: OutputStreamPath
Posted by Laurie Harper <la...@holoweb.net>.
Dave Newton wrote:
> --- Daniel Chacón Sánchez <da...@gmail.com>
>> I have a question, how I can set the path for the
>> OutputStream like i get it for the InputStream with
>> getResourceAsStream?
>
> I'm not sure you can do that, particularly since a
> resource might be inside a JAR file or be located in a
> place you do not have write acess to.
Yup, there's no guarantee that the contents of a webapp can be written
to in all deployment contexts. However, as long as you know you will be
deploying it to a container that allows the file to be written to, you
should be OK.
> If you know that the properties file is in a specific
> place and that it makes sense to write to it then I
> would think you'd use typical file i/o and determine
> the path via getRealPath.
In your case, a simple change to the code you've already written should
do the trick. Something like this (untested):
URL res = Thread.currentThread()
.getContextClassLoader()
.getResource(pathToConfigurationFile);
InputStream input = new FileInputStream(res);
//Load the properties
properties.load(input);
input.close();
...
// Save the properties
OutputStream out = new FileOutputStream(res);
properties.store(out, "");
> It confuses me why you'd want to do this in a webapp,
> though. Settings you are likely to modify from within
> the app should be in a database, not a properties file
> (IMO, anyway).
Agreed. Or otherwise externalized, for example through JNDI or something.
L.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: OutputStreamPath
Posted by Dave Newton <ne...@yahoo.com>.
--- Daniel Chacón Sánchez <da...@gmail.com>
wrote:
> Someone?
Sorry it took me so long.
> I have a question, how I can set the path for the
> OutputStream like i get it for the InputStream with
> getResourceAsStream?
I'm not sure you can do that, particularly since a
resource might be inside a JAR file or be located in a
place you do not have write acess to.
If you know that the properties file is in a specific
place and that it makes sense to write to it then I
would think you'd use typical file i/o and determine
the path via getRealPath.
It confuses me why you'd want to do this in a webapp,
though. Settings you are likely to modify from within
the app should be in a database, not a properties file
(IMO, anyway).
d.
____________________________________________________________________________________
Sucker-punch spam with award-winning protection.
Try the free Yahoo! Mail Beta.
http://advision.webevents.yahoo.com/mailbeta/features_spam.html
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
OutputStreamPath
Posted by Daniel Chacón Sánchez <da...@gmail.com>.
Someone?
Hi all
I have a question, how I can set the path for the OutputStream like i get it
for the InputStream with getResourceAsStream?
This is my code:
Properties properties = new Properties();
String pathToConfigurationFile =
"common/properties/Configuracion.properties";
InputStream input =
Thread.currentThread().getContextClassLoader().getResourceAsStream(
pathToConfigurationFile);
//Load the properties
properties.load(input);
input.close ();
//Set the property datasource
properties.setProperty("datasource", datasource);
//Now I want to save the changes to the properties file
OutputStream out= new FileOutputStream(
pathToConfigurationFile);
properties.store(out, "");
//But an exception occurs and it tells me that the
"common/properties/Configuracion.properties" was not found.
How I can get the correct path for the outputStream like i did with the
inputStream
Thanks!!