0

我使用 PrintWriter out 对象。我写数据 out.println(some data) 并用 out.close 关闭它

URL url = new URL(myurl);
URLConnection connection = null;
PrintWriter out = null; BufferedReader br = null; connection = url.openConnection(); connection.setDoOutput(true);
out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream()),true);
while(iterations) { 
    //print data on writer 
    out.println(object);
 } 
 //closig print writer
 out.flush(); 
 out.close(); 
 //Response from server
 br = new BufferedReader(new InputStreamReader(connection.getInputStream())); // Get Exception in //this line EOF Exception
 String temp;
 while(temp = br.readLine() !=null) { 
     //do something 
 }
 br.close();
4

1 回答 1

0
URL url = new URL(myurl);
 URLConnection connection = null;
 PrintWriter out = null; BufferedReader br = null; connection = url.openConnection(); connection.setDoOutput(true);
 out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream()),true);
 while(iterations)
     { 
//print data on writer 
out.println(object);
 } 
//closig print writer
 out.flush(); 

//Response from server
 br = new BufferedReader(new InputStreamReader(connection.getInputStream())); // Get Exception in //this line EOF Exception
 String temp;
 while(temp = br.readLine() !=null)
{ //do something }
 out.close();

看看代码,你只需要在最后放 out.close 而不是 br.close。

于 2012-11-19T11:55:54.483 回答