Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How  to save a string to an output file

Posted on 2003-03-19
8
Medium Priority
?
166 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What happened when I tried to make my phone, a Samsung Galaxy S8, stop dropping all WiFi signals was a saga I won't repeat here, but what you might want to know is how it started and how I got it working again.
This blog will spread awareness about Dropbox. We have given the statements based upon our experience. Along with this, there is a section of some new plans that should be added in Dropbox this year. This will make the storage service enhanced from …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

571 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