?
Solved

How  to save a string to an output file

Posted on 2003-03-19
8
Medium Priority
?
163 Views
Last Modified: 2012-06-21
Dear experts,
I have a JSP page that is displaying some information that was got from database. But I would like to get this information and save to an output file.

I created a Utility class that has a method called GenerateFileOutput() that contains the contains bellow:
DataOutputStream outFile = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(theFileName)));
         outFile.writeBytes(theOutputStr);
            outFile.close();

My JSP contains the line bellow:
<% util.GenerateFileOutput("/abc.txt", m_sAllFileContents); %>

My JSP is executed sucessfully, but the output file is not being generated.

Is there another way to do this in JSP?

Regards,

Carla

 
0
Comment
Question by:cpribeiro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 14

Expert Comment

by:kennethxu
ID: 8167340
don't use DataOutputStream, it is for you to store java object, not the raw data, try this:

OutputStream outFile = new FileOutputStream(theFileName);
outFile.write( theOutputStr.getBytes() );
outFile.close();

I assumed "theOutputStr" is String (as it named). In fact, you should always use byte array to store file contents. converting to and/or from String can destroy your binary file content.


0
 

Author Comment

by:cpribeiro
ID: 8167765
Dear kennethxu,

Thank you for help.

I modified the file following your instruction. I changed the code to:

OutputStream outFile = new FileOutputStream(theFileName);
outFile.write( theOutputStr.getBytes() );
outFile.close();

But the file was not created. Why is happening this? I searched in all hard disk where the OC4j (JSP Server) is installed, but nothing was created. Please, help me....

Regards,

Carla
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8168306
is there any exception thrown? if yes, please post stacktrace of the error. you can also try to print out something for debuging:
System.out.println( "the file name is: " + theFileName );
OutputStream outFile = new FileOutputStream(theFileName);
System.out.println( "writing to file" );
outFile.write( theOutputStr.getBytes() );
System.out.println( "close the file" );
outFile.close();

BTW, can you post entire code of method GenerateFileOutput?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:cpribeiro
ID: 8168378
Dear kennethxu,
When I write in my code:
OutputStream outFile = new FileOutputStream("abc.txt");
outFile.write( theOutputStr.getBytes() );
outFile.close();

The JSP file is executed, but nothing is generated...

But If Write in my code:
File outputFile = new File("default-web-app\\bench\\abc.txt");
FileWriter fwOut = new FileWriter(outputFile);
fwOut.write(theOutputStr);
fwOut.close();

The following exception is being displayed:
java.lang.IllegalStateException: Response has already been committed
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.EvermindHttpServletResponse.sendError(EvermindHttpServletResponse.java:1100)
     at oracle.security.jazn.oc4j.JAZNServletResponse.sendError(JAZNServletResponse.java:87)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.ErrorRequestDispatcher.include(ErrorRequestDispatcher.java:35)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.EvermindPageContext.handlePageThrowable(EvermindPageContext.java:596)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.EvermindPageContext.handlePageException(EvermindPageContext.java:567)
     at _bench._ActivitiesByUser._jspService(_ActivitiesByUser.java:145)
     [SRC:/bench/ActivitiesByUser.jsp]
     at com.orionserver[Oracle9iAS (9.0.2.0.0) Containers for J2EE].http.OrionHttpJspPage.service(OrionHttpJspPage.java:56)
     at oracle.jsp.runtimev2.JspPageTable.compileAndServe(JspPageTable.java:508)
     at oracle.jsp.runtimev2.JspPageTable.service(JspPageTable.java:255)
     at oracle.jsp.runtimev2.JspServlet.internalService(JspServlet.java:407)
     at oracle.jsp.runtimev2.JspServlet.service(JspServlet.java:330)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:336)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.ResourceFilterChain.doFilter(ResourceFilterChain.java:59)
     at oracle.security.jazn.oc4j.JAZNFilter.doFilter(JAZNFilter.java:283)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:523)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:269)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:735)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.HttpRequestHandler.run(HttpRequestHandler.java:243)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].util.ThreadPoolThread.run(ThreadPoolThread.java:64)

Some idea?

Regards,
Carla
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8168516
you should use absolute path, when you use relative path, the result is undefined. it is NOT relative to your jsp page.

try

OutputStream outFile = new FileOutputStream("C:/abc.txt");
outFile.write( theOutputStr.getBytes() );
outFile.close();

and see what you get? thanks.
0
 

Author Comment

by:cpribeiro
ID: 8168619
Dear kennethxu,
When I write in my code:
OutputStream outFile = new FileOutputStream("abc.txt");
outFile.write( theOutputStr.getBytes() );
outFile.close();

The JSP file is executed, but nothing is generated...

But If Write in my code:
File outputFile = new File("default-web-app\\bench\\abc.txt");
FileWriter fwOut = new FileWriter(outputFile);
fwOut.write(theOutputStr);
fwOut.close();

The following exception is being displayed:
java.lang.IllegalStateException: Response has already been committed
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.EvermindHttpServletResponse.sendError(EvermindHttpServletResponse.java:1100)
     at oracle.security.jazn.oc4j.JAZNServletResponse.sendError(JAZNServletResponse.java:87)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.ErrorRequestDispatcher.include(ErrorRequestDispatcher.java:35)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.EvermindPageContext.handlePageThrowable(EvermindPageContext.java:596)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.EvermindPageContext.handlePageException(EvermindPageContext.java:567)
     at _bench._ActivitiesByUser._jspService(_ActivitiesByUser.java:145)
     [SRC:/bench/ActivitiesByUser.jsp]
     at com.orionserver[Oracle9iAS (9.0.2.0.0) Containers for J2EE].http.OrionHttpJspPage.service(OrionHttpJspPage.java:56)
     at oracle.jsp.runtimev2.JspPageTable.compileAndServe(JspPageTable.java:508)
     at oracle.jsp.runtimev2.JspPageTable.service(JspPageTable.java:255)
     at oracle.jsp.runtimev2.JspServlet.internalService(JspServlet.java:407)
     at oracle.jsp.runtimev2.JspServlet.service(JspServlet.java:330)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:336)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.ResourceFilterChain.doFilter(ResourceFilterChain.java:59)
     at oracle.security.jazn.oc4j.JAZNFilter.doFilter(JAZNFilter.java:283)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:523)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:269)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:735)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].server.http.HttpRequestHandler.run(HttpRequestHandler.java:243)
     at com.evermind[Oracle9iAS (9.0.2.0.0) Containers for J2EE].util.ThreadPoolThread.run(ThreadPoolThread.java:64)

Some idea?

Regards,
Carla
0
 

Author Comment

by:cpribeiro
ID: 8168646
Dear kennethxu,

Now, the file was generated successfully....
Thank you for reply...

But before I had put this solution, but instead of ("C:/abc.txt"), I had put ("c:\abc.txt") and nothing was being generated.

Thanks again...

Regards,

Carla

0
 
LVL 14

Accepted Solution

by:
kennethxu earned 80 total points
ID: 8171139
glad to know your problem is solved. are you ready to close this question?
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question