You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Garrett Smith <ja...@yahoo.com> on 2002/11/08 01:39:27 UTC

[tomcat & java.io.File] Reading a file from /WEB-INF/classes

Hello Everyone,

I want to read the file /WEB-INF/classes/confirmation.txt

The file is read from 
/WEB-INF/classes/com/dhtmlkitchen/reg/mail/FileFormatter.java


File formatter.java snip:


class FileFormatter {

	private FileFormatter(){}
	
	/** formatFile accepts a filename (as a <code>String</code>) and 
	 *  an <code>Object[]</code>
	 *
	 *  expects that the file will need an 
	 *  <code>Object[]</code> with 4 params:
	 * 
	 * <ul>
	 * <li> the URL where the user signed up at </li>
	 * <li> username </li>
	 * <li> password </li>
	 * <li> the uid </li>
	 * </ul>
	 *
	 *  @see java.text.MessageFormat
	 *  @return java.lang.String
	 */ 
	protected static String formatFile(String filename, Object[] o) throws
IOException {
		return MessageFormat.format(fileToString(new File(filename)), o);
	}

	/** fileToString accepts a java.io.File
	 *  and returns a String with the text of that file. 
	 *
	 *  @see java.io.File
	 *  @return java.lang.String
	 *  @throw java.io.IOException
	 */ 
	private static String fileToString(File file) throws IOException{

		StringBuffer sb = new StringBuffer();

		// throws java.io.IOException
		BufferedReader in = new BufferedReader(new FileReader(file));
		for (String s;(s = in.readLine()) != null;sb.append(s+"\r\n"));
		in.close();
		
		return sb.toString();
	
	}	


Which argument do I pass to FileFormatter.formatFile?

A. "confirmation.txt"
B. "../../../../confirmation.txt"
C. other (write in __________ ).

I am getting mixed results with A. "confirmation.txt": it works from home, but
not on my host. I haven't tried B. If A. works at home, then B. won't. I'm not
into guesswork, so I didn't try C., either. I came hear for a minor education
and to solve my problem.

Thank you,

Garrett

=====
http://dhtmlkitchen.com/
JSP | Servlets | DHTML 

Garrett Needs A Job

__________________________________________________
Do you Yahoo!?
HotJobs - Search new jobs daily now
http://hotjobs.yahoo.com/

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [tomcat & java.io.File] Reading a file from /WEB-INF/classes

Posted by Garrett Smith <ja...@yahoo.com>.
Ah, 

Option D. Thank you, Justin. That's what I wanted but didn't realize it.

Thanks again,

Garrett


--- Justin Ruthenbeck <ju...@nextengine.com> wrote:
> 
> Garrett --
> 
> Hopefully I understand you correctly ... I'm pretty sure I do.  Regardless 
> of whether FileFormatter is a servlet, you can still use
> 
> InputStream in = this.getClass().getResourceAsStream("confirmation.txt");
> 
> to load the file and do whatever you want with the *contents* of it 
> (hopefully you don't need to do anything except read it).  The only 
> difference between doing this and instantiating a File object (to read the 
> file's data) is that this will retrieve the file from the stack of 
> ClassLoaders instead of from a pre-determined location.  Since you've got 
> the confirmation.txt file in the WEB-INF/classes
> directory, it'll pick it up fine.
> 
> So, choose option "D":
> 
> D. Add method FileFormatter.format(InputStream in) to take a stream instead 
> of a File object. Then use the above code and pass that stream into the 
> FileFormatter.  Hopefully you have access to this.  If not, take the 
> stream, create a new File, and pass that File along.
> 
> 
> -=justin=-
> 
> At 06:03 PM 11/7/2002, you wrote:
> >This is probably more an I/O question than a tomcat question.
> >
> >Take a look at my first post. FileFormatter is not a servlet. I'll do my
> best
> >to explain.
> >
> >Works like this:
> >
> >1) Controller servlet -> (com.dhtmlkitchen.Registration)
> >
> >   - uses objects to preprocess data. It data check passes...
> >   - creates an object (bean) that is passed to Mailer.sendConfirmation.
> (does
> >some other stuff not worth mention)
> >
> >     2)  -- Mailer.sendConfirmation :: void - opens, formats, and sends a 
> > file.
> >        -- The filename is found in mail.properties, the "mail.text.file"
> >property of mail.properties. (inited in a static block). the filename is
> >"mail.text.file = confirmation.txt". The file opening is delegated to ->
> >
> >     3) FileFormatter
> >      formatFile :: String - opens and formats the file
> >
> >
> >thats how it works.
> >
> >Mailer.java has no problem finding mail.properties, but FileFormatter.java
> has
> >a problem finding confirmation.txt.
> >
> >See:
> >
> >[localhost:dhtmlkitchen_dev/WEB-INF/classes] garrett% ls
> >  confirmation.txt     mail.properties
> >  -et c.
> >
> >QUESTION:
> >
> >Which argument do I pass to FileFormatter.formatFile?
> >
> >A. "confirmation.txt"
> >B. "../../../../confirmation.txt"
> >C. other (write in __________ ).
> >
> >
> >PLEASE SEE MY PREVIOUS POST FOR A MORE CONCISE EXPLANATION!
> >
> >
> >
> >
> >--- Justin Ruthenbeck <ju...@nextengine.com> wrote:
> > >
> > > ... and check out this thread, which goes into extreme detail and a
> couple
> > > different ways to do it...
> > >
> > > http://www.mail-archive.com/tomcat-user@jakarta.apache.org/msg68849.html
> > >
> > > justin
> > >
> > > At 04:43 PM 11/7/2002, you wrote:
> > > >Hiya Garrett,
> > > >
> > > >You're not guaranteed to have a filesystem to read files from.
> > > >
> > > >The WAR file may not be unpacked, or it may be stored in the
> > > >brains of a million monkeys which are not directly addressable
> > > >using a java.io.File call.
> > > >
> > > >All you know is that you can access this file using:
> > > >
> > > >getResourceAsStream("/confirmation.txt")
> > > >
> > > >Have a look at the javadoc for ServletContext
> > > >
> > > >Cheers,
> > > >         -- jon
> > > >
> > > >Garrett Smith wrote:
> > > > > Hello Everyone,
> > > > >
> > > > > I want to read the file /WEB-INF/classes/confirmation.txt
> > > > >
> > > > > The file is read from
> > > > > /WEB-INF/classes/com/dhtmlkitchen/reg/mail/FileFormatter.java
> > > >
> > > >
> > > >--
> > > >Jon Eaves <jo...@eaves.org>
> > > >http://www.eaves.org/jon/
> > > >
> > > >
> > > >
> > > >--
> > > >To unsubscribe, e-mail:
> > > <ma...@jakarta.apache.org>
> > > >For additional commands, e-mail:
> > > <ma...@jakarta.apache.org>
> > >
> > >
> > > --
> > > To unsubscribe, 
> > e-mail:   <ma...@jakarta.apache.org>
> > > For additional commands, e-mail: 
> > <ma...@jakarta.apache.org>
> > >
> >
> >
> >=====
> >http://dhtmlkitchen.com/
> >JSP | Servlets | DHTML
> >
> >Garrett Needs A Job
> >
> >__________________________________________________
> >Do you Yahoo!?
> >U2 on LAUNCH - Exclusive greatest hits videos
> >http://launch.yahoo.com/u2
> >
> >--
> >To unsubscribe, e-mail:  
> <ma...@jakarta.apache.org>
> >For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 


=====
http://dhtmlkitchen.com/
JSP | Servlets | DHTML 

Garrett Needs A Job

__________________________________________________
Do you Yahoo!?
U2 on LAUNCH - Exclusive greatest hits videos
http://launch.yahoo.com/u2

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [tomcat & java.io.File] Reading a file from /WEB-INF/classes

Posted by Justin Ruthenbeck <ju...@nextengine.com>.
Garrett --

Hopefully I understand you correctly ... I'm pretty sure I do.  Regardless 
of whether FileFormatter is a servlet, you can still use

InputStream in = this.getClass().getResourceAsStream("confirmation.txt");

to load the file and do whatever you want with the *contents* of it 
(hopefully you don't need to do anything except read it).  The only 
difference between doing this and instantiating a File object (to read the 
file's data) is that this will retrieve the file from the stack of 
ClassLoaders instead of from a pre-determined location.  Since you've got 
the confirmation.txt file in the WEB-INF/classes
directory, it'll pick it up fine.

So, choose option "D":

D. Add method FileFormatter.format(InputStream in) to take a stream instead 
of a File object. Then use the above code and pass that stream into the 
FileFormatter.  Hopefully you have access to this.  If not, take the 
stream, create a new File, and pass that File along.


-=justin=-

At 06:03 PM 11/7/2002, you wrote:
>This is probably more an I/O question than a tomcat question.
>
>Take a look at my first post. FileFormatter is not a servlet. I'll do my best
>to explain.
>
>Works like this:
>
>1) Controller servlet -> (com.dhtmlkitchen.Registration)
>
>   - uses objects to preprocess data. It data check passes...
>   - creates an object (bean) that is passed to Mailer.sendConfirmation. (does
>some other stuff not worth mention)
>
>     2)  -- Mailer.sendConfirmation :: void - opens, formats, and sends a 
> file.
>        -- The filename is found in mail.properties, the "mail.text.file"
>property of mail.properties. (inited in a static block). the filename is
>"mail.text.file = confirmation.txt". The file opening is delegated to ->
>
>     3) FileFormatter
>      formatFile :: String - opens and formats the file
>
>
>thats how it works.
>
>Mailer.java has no problem finding mail.properties, but FileFormatter.java has
>a problem finding confirmation.txt.
>
>See:
>
>[localhost:dhtmlkitchen_dev/WEB-INF/classes] garrett% ls
>  confirmation.txt     mail.properties
>  -et c.
>
>QUESTION:
>
>Which argument do I pass to FileFormatter.formatFile?
>
>A. "confirmation.txt"
>B. "../../../../confirmation.txt"
>C. other (write in __________ ).
>
>
>PLEASE SEE MY PREVIOUS POST FOR A MORE CONCISE EXPLANATION!
>
>
>
>
>--- Justin Ruthenbeck <ju...@nextengine.com> wrote:
> >
> > ... and check out this thread, which goes into extreme detail and a couple
> > different ways to do it...
> >
> > http://www.mail-archive.com/tomcat-user@jakarta.apache.org/msg68849.html
> >
> > justin
> >
> > At 04:43 PM 11/7/2002, you wrote:
> > >Hiya Garrett,
> > >
> > >You're not guaranteed to have a filesystem to read files from.
> > >
> > >The WAR file may not be unpacked, or it may be stored in the
> > >brains of a million monkeys which are not directly addressable
> > >using a java.io.File call.
> > >
> > >All you know is that you can access this file using:
> > >
> > >getResourceAsStream("/confirmation.txt")
> > >
> > >Have a look at the javadoc for ServletContext
> > >
> > >Cheers,
> > >         -- jon
> > >
> > >Garrett Smith wrote:
> > > > Hello Everyone,
> > > >
> > > > I want to read the file /WEB-INF/classes/confirmation.txt
> > > >
> > > > The file is read from
> > > > /WEB-INF/classes/com/dhtmlkitchen/reg/mail/FileFormatter.java
> > >
> > >
> > >--
> > >Jon Eaves <jo...@eaves.org>
> > >http://www.eaves.org/jon/
> > >
> > >
> > >
> > >--
> > >To unsubscribe, e-mail:
> > <ma...@jakarta.apache.org>
> > >For additional commands, e-mail:
> > <ma...@jakarta.apache.org>
> >
> >
> > --
> > To unsubscribe, 
> e-mail:   <ma...@jakarta.apache.org>
> > For additional commands, e-mail: 
> <ma...@jakarta.apache.org>
> >
>
>
>=====
>http://dhtmlkitchen.com/
>JSP | Servlets | DHTML
>
>Garrett Needs A Job
>
>__________________________________________________
>Do you Yahoo!?
>U2 on LAUNCH - Exclusive greatest hits videos
>http://launch.yahoo.com/u2
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [tomcat & java.io.File] Reading a file from /WEB-INF/classes

Posted by Garrett Smith <ja...@yahoo.com>.
This is probably more an I/O question than a tomcat question.

Take a look at my first post. FileFormatter is not a servlet. I'll do my best
to explain.

Works like this:

1) Controller servlet -> (com.dhtmlkitchen.Registration)

  - uses objects to preprocess data. It data check passes...
  - creates an object (bean) that is passed to Mailer.sendConfirmation. (does
some other stuff not worth mention)

    2)  -- Mailer.sendConfirmation :: void - opens, formats, and sends a file. 
       -- The filename is found in mail.properties, the "mail.text.file"
property of mail.properties. (inited in a static block). the filename is
"mail.text.file = confirmation.txt". The file opening is delegated to ->

    3) FileFormatter
     formatFile :: String - opens and formats the file 
     

thats how it works.

Mailer.java has no problem finding mail.properties, but FileFormatter.java has
a problem finding confirmation.txt.

See:

[localhost:dhtmlkitchen_dev/WEB-INF/classes] garrett% ls
 confirmation.txt     mail.properties
 -et c.

QUESTION: 

Which argument do I pass to FileFormatter.formatFile?

A. "confirmation.txt"
B. "../../../../confirmation.txt"
C. other (write in __________ ).


PLEASE SEE MY PREVIOUS POST FOR A MORE CONCISE EXPLANATION!


     
      
--- Justin Ruthenbeck <ju...@nextengine.com> wrote:
> 
> ... and check out this thread, which goes into extreme detail and a couple 
> different ways to do it...
> 
> http://www.mail-archive.com/tomcat-user@jakarta.apache.org/msg68849.html
> 
> justin
> 
> At 04:43 PM 11/7/2002, you wrote:
> >Hiya Garrett,
> >
> >You're not guaranteed to have a filesystem to read files from.
> >
> >The WAR file may not be unpacked, or it may be stored in the
> >brains of a million monkeys which are not directly addressable
> >using a java.io.File call.
> >
> >All you know is that you can access this file using:
> >
> >getResourceAsStream("/confirmation.txt")
> >
> >Have a look at the javadoc for ServletContext
> >
> >Cheers,
> >         -- jon
> >
> >Garrett Smith wrote:
> > > Hello Everyone,
> > >
> > > I want to read the file /WEB-INF/classes/confirmation.txt
> > >
> > > The file is read from
> > > /WEB-INF/classes/com/dhtmlkitchen/reg/mail/FileFormatter.java
> >
> >
> >--
> >Jon Eaves <jo...@eaves.org>
> >http://www.eaves.org/jon/
> >
> >
> >
> >--
> >To unsubscribe, e-mail:  
> <ma...@jakarta.apache.org>
> >For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 


=====
http://dhtmlkitchen.com/
JSP | Servlets | DHTML 

Garrett Needs A Job

__________________________________________________
Do you Yahoo!?
U2 on LAUNCH - Exclusive greatest hits videos
http://launch.yahoo.com/u2

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [tomcat & java.io.File] Reading a file from /WEB-INF/classes

Posted by Justin Ruthenbeck <ju...@nextengine.com>.
... and check out this thread, which goes into extreme detail and a couple 
different ways to do it...

http://www.mail-archive.com/tomcat-user@jakarta.apache.org/msg68849.html

justin

At 04:43 PM 11/7/2002, you wrote:
>Hiya Garrett,
>
>You're not guaranteed to have a filesystem to read files from.
>
>The WAR file may not be unpacked, or it may be stored in the
>brains of a million monkeys which are not directly addressable
>using a java.io.File call.
>
>All you know is that you can access this file using:
>
>getResourceAsStream("/confirmation.txt")
>
>Have a look at the javadoc for ServletContext
>
>Cheers,
>         -- jon
>
>Garrett Smith wrote:
> > Hello Everyone,
> >
> > I want to read the file /WEB-INF/classes/confirmation.txt
> >
> > The file is read from
> > /WEB-INF/classes/com/dhtmlkitchen/reg/mail/FileFormatter.java
>
>
>--
>Jon Eaves <jo...@eaves.org>
>http://www.eaves.org/jon/
>
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [tomcat & java.io.File] Reading a file from /WEB-INF/classes

Posted by Jon Eaves <jo...@eaves.org>.
Hiya Garrett,

You're not guaranteed to have a filesystem to read files from.

The WAR file may not be unpacked, or it may be stored in the
brains of a million monkeys which are not directly addressable
using a java.io.File call.

All you know is that you can access this file using:

getResourceAsStream("/confirmation.txt")

Have a look at the javadoc for ServletContext

Cheers,
	-- jon

Garrett Smith wrote:
 > Hello Everyone,
 >
 > I want to read the file /WEB-INF/classes/confirmation.txt
 >
 > The file is read from
 > /WEB-INF/classes/com/dhtmlkitchen/reg/mail/FileFormatter.java


-- 
Jon Eaves <jo...@eaves.org>
http://www.eaves.org/jon/



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>