I need to figure out a way to write the contents of a JSF inpuText to a file. I want to build a small UI which will offer the user the posibility to write some math formulas in an easier way(hence the UI) and be able to save them to a .txt (or something similar) file. I have been trying for about 8-10 hours now and the best I've got is:
<h:inputText value="#{output.inputContent}" id="inputContent"></h:inputText>
And the Bean:
import com.sun.faces.taglib.html_basic.InputTextTag;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.servlet.http.HttpServletRequest;
@ManagedBean(name = "output")
@SessionScoped
public class OutputHandler {
public String inputContent;
public String getinputContent() {
return inputContent;
}
public void setinputContent(String inputContent) {
this.inputContent = inputContent;
}
public void writeStream()
{
try{
FileOutputStream fos=new FileOutputStream("outputOne.txt");
ObjectOutputStream output=new ObjectOutputStream(fos);
output.writeObject(inputContent);
}
catch(Exception ex){
开发者_Go百科 System.out.println("Exception is: "+ex);
}
}
}
I'm pretty sure most of what I did there is wrong so any help would be appreciated, even if it is some directions to posts which treat simmilar issues so I can try and get it from there. I don't want to be spoonfed or anything, the problem is I have never worked with JSF(I started reading about it yesterday) so as I said, even some links to some documentation which could help me(I've been googling like crazy since I started) or guide me a bit will be greatly appreciated.
With ObjectOutputStream
you're basically serializing a String
object to the output. The result is not a human readable text file with string's original value.
You need to use a Writer
wherein you pass the string's value to.
public void writeStream() throws IOException {
Writer writer = new FileWriter("outputOne.txt");
try {
writer.write(inputContent);
} finally {
writer.close();
}
}
This will end up in a human readable text file with the string's original value.
Unrelated to the concrete problem, please note that using relative paths in file IO is an extremely bad idea. You never know for sure where it will end up and in some environments the working directory is not writable as well. Always explicitly specify an absolute path to an existing and writable folder:
Writer writer = new FileWriter("/path/to/outputOne.txt");
In Windows environments where the server is installed and executed from the C:\
disk, the above path is basically the same as C:\path\to\outputOne.txt
. You can if necessary configure the folder root as VM argument or configuration file setting. Assuming that you pass -Doutput.location=/path/to
VM argument, then you could use it as follows:
File root = new File(System.getProperty("output.location"));
Writer writer = new FileWriter(new File(root, "outputOne.txt"));
精彩评论