aspose file tools*
The moose likes I/O and Streams and the fly likes closing streams Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "closing streams" Watch "closing streams" New topic
Author

closing streams

peter brews
Ranch Hand

Joined: Jan 25, 2002
Posts: 59
Dear All,
Could you tell me if i am closing the inputstream ,outputstream etc..
correctly or not. I want to ensure that all the open streams gets closed(and also flushed )
properly.I am closing
out.close in try block and also in finally block also. Is it correct to do so OR should i close out,out1,in only in finally. Also what if don't use out.flush().
Following is my code:
import javax.servlet.*;
import javax.servlet.http.*;
...
..
public class SServlet extends HttpServlet
{
private static final String CONTENT_TYPE = "text/xml" ;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException
{
try {
String urlname = "xyz";
PrintWriter out = response.getWriter();
BufferedReader in = null;
String Name = request.getParameter("name");
String arg0=request.getParameter("arg0");
String REQUEST = "";
if(Name != null & Name != "")
{
if(Name.equals("Robert"))
{
REQUEST="aaaaaaaaaaaaaaa";
..
...
}
else if (Name.equals("smith"))
{
..
REQUEST="bbbbbbbbbbb";
..
}
}
else{

..
}
URL url = new URL(urlname);
HttpURLConnection connection =(HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-length", "" +REQUEST.length());
connection.setRequestProperty("Content-type", "text/xml");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
PrintWriter out1 = new PrintWriter(connection.getOutputStream());
out1.print(REQUEST);
out1.close();
in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
out.print(inputLine);
}
in.close();
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
finally{
out1.close();
out.close();
try{
if (in!=null){
in.close();
}
}catch(Exception e){
e.printStackTrace();
}

}
}
}
Thanks,
peter
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Please add [code] tags to your post and use proper indentation to make your code readable - it's very hard to follow otherwise. Because of this I didn't look too closely, but I will note that if you call close(), flush() is called automatically, so no need to call it again. And if you call close() from inside a finally clause (the best place, almost always) there's no need to also call it outside the finally. Once is enough.


"I'm not back." - Bill Harding, Twister
peter brews
Ranch Hand

Joined: Jan 25, 2002
Posts: 59
Thanks for your advice .
I ahve formatted the code:

Thanks,
peter
[ December 13, 2002: Message edited by: Jim Yingst ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: closing streams