BufferedReader 和BufferedWriter

BufferedWriter:

private void test(String content,String destPath) throws IOException
{
BufferedReader br = null;
BufferedWriter bufw= null;
try
{
br = new BufferedReader(new StringReader(content));
bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destPath),"UTF-8"));

char[] buf = new char[2048];
int len = 0;
while((len = br.read(buf)) != -1)
{
bufw.write(buf,0,len);
}
bufw.flush();
}
catch (FileNotFoundException e)
{
log.error(e);
throw new ValidateException("error.adobepass.location", null, destPath);
}
finally
{
IOUtil.close(bufw);
IOUtil.close(br);
}
}

BufferedReader :

public String getHttpUrlContent(String url)
{
HttpClient client = new HttpClient();
HttpClientParams param = new HttpClientParams();
param.setSoTimeout(60000);
param.setConnectionManagerTimeout(60000);
client.setParams(param);
GetMethod method = new GetMethod(url);
try
{
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK)
{
log.debug("Access " + url + " failed, return null, statusCode: " + method.getStatusLine());
return null;
}
log.debug("Access " + url);
InputStream is = method.getResponseBodyAsStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null)
{
buffer.append(line + "\n");
}
return buffer.toString();
}
catch (Exception e)
{
e.printStackTrace();
log.error("Access " + url + " failed: " + e.getMessage());
return null;
}
finally
{
method.releaseConnection();
}
}

猜你喜欢

转载自www.cnblogs.com/daxiong225/p/8961081.html
今日推荐