File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Java code File and FileWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java code File and FileWriter" Watch "Java code File and FileWriter" New topic
Author

Java code File and FileWriter

Sana Ali
Greenhorn

Joined: Oct 08, 2004
Posts: 14
Hi I am using DOM to make an xml tree and writing it to a file. The code involves use of java.

I have declared the fileName of the file:

String fileName = "/tmp/memberFile";

to save it to the file i have done the following:

File membersXMLFile = new File(fileName);
FileWriter writer;

if (!(membersXMLFile.exists()))
{
writer = new FileWriter(membersXMLFile);
}
else //file does exist
{
writer = new FileWriter(membersXMLFile, true);
}


if file does not exist FileWriter writes to file. else if it does exist, file writer writes to the file by appended to the file.

However, i am still getting a TOMCAT Error. As this is java code. I am confused on what is wrong with my code.

Please Help
Sana
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
you should be get path of current directory/floder in server at runtime.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Sana Ali
Greenhorn

Joined: Oct 08, 2004
Posts: 14
but im told to set it to "/tmp/fileName.xml"

as TOMCAT server runs there, other wise the jsp file cannot be executed.

Anyway, my Q was about my java code...does it even make sense?
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Oh, i'm sorry, i'm wrong ::

you should get absolute path of current directory Before read/create your files...
[ October 15, 2004: Message edited by: somkiat puisungnoen ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You haven't told us what error you're getting. If the error page doesn't include a stack trace, have you looked in catalina.out, and theother log files, for stack traces? Let us know what the actual error is, and maybe we can help. Otherwise, we're just fumbling around here -- there's nothing wrong with the code intrinsically, it's just a question of whether it does what you want it to do.

Note that appending to an existing XML file is just going to give you a broken XML file, by the way.


[Jess in Action][AskingGoodQuestions]
Sana Ali
Greenhorn

Joined: Oct 08, 2004
Posts: 14
error is:

type Exception report

message Internal Server Error

description The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.

exception
javax.servlet.ServletException
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:471)


javax.servlet.ServletException
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:471)
at org.apache.jsp.memberdisplayWithDom$jsp._jspService(memberdisplayWithDom$jsp.java:542)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:201)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:381)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:473)


root cause

java.lang.OutOfMemoryError
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
This is error in process compile jsp to SErvlet class of Jasper.

And OutOfMemoryError is occured from
- infinity loop.
- Server memory/heap size not enough.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java code File and FileWriter