Writing to a file

Hello all,

this is my first message to the forums, and I'm posting here because I have run into a problem developing my first Java based web app.

I would like to have data that is submitted be written to a file.

To do so I have the JSP page access its corresponding JavaBean.

One of the methods that is called along the way in the JavaBean has been set up to write to a file using a PrintWriter object.

The file however is never generated. I tried the code with a java project and it works fine, outputing the file as I'd expect.

Is there something I should be doing differently when trying to do the same thing using JSPs and JavaBeans?

Thanks,

-Alex

[710 byte] By [mon.goosea] at [2007-11-27 6:52:47]
# 1
Can you post the relevant code?
tolmanka at 2007-7-12 18:27:23 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Sure,

String file = "output_05.txt";

String toPrint = "string sample";

PrintWriter out = new PrintWriter(file);

out.println("The sample string is: " + toPrint);

out.close();

return "The JavaBean has written "+toPrint+" to the file "+file;

this method [getWriteFile()] throws IOException which is not included above

to call the method I have a tag like this:

<h:outputText id="beanResponse" value="#{FileWriter.writeFile}"/>

I feel like I'm really gluing things together here, there must be a better way to call a JavaBean method.

I hope I'm not disturbing anyone by writing to both the JSF and JSP forums, I feel it's relevant to both…

Thanks again.

mon.goosea at 2007-7-12 18:27:23 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

You should post a message only once. Pick one forum you think is most related, and go with that one. A lot of us read multiple forums anyway, and it is annoying to see the same question multiple times.

If it really IS relevant to multiple forums, post it in one forum, and then if you don't get a response, post a link/pointer to it into the other forum. That way you keep all the answers in one place.

Anyways, I think this question probably belongs in basic java programming rather than JSP/JSF. As far as I can tell, this won't even compile.

There is no constructor of PrintWriter which takes a String, or even a File as a parameter.

Where on disk do you want this file to be generated?

To create a file you should be using a java.io.FileWriter object.

See the examples [url http://java.sun.com/docs/books/tutorial/essential/io/charstreams.html]here[/url]

evnafetsa at 2007-7-12 18:27:23 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

I now realise how closely coupled the two forums I posted in are. I apologize for cross posting and so I plan to use this one from now on. I would post this in the basic java section, except that my problems are directly related to implementing the code in a web application.

What frustrates me so much is that I've tried running the relevant code in a seperate java class. It compiles and does what I expect it to, and that's to create a new file and write to it.

mon.goosea at 2007-7-12 18:27:23 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...