• 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 + " ");
    }
    return buffer.toString();
    }
    catch (Exception e)
    {
    e.printStackTrace();
    log.error("Access " + url + " failed: " + e.getMessage());
    return null;
    }
    finally
    {
    method.releaseConnection();
    }
    }

  • 相关阅读:
    数据库连接池
    JDBC事务
    oracle 11g
    python自动化办公1-os模块学习
    python模块学习1
    requests-post请求
    linux学习二-目录文件相关命令
    Linux学习一常见的7个命令及命令的信息查看
    python-文件操作
    异常以及异常处理
  • 原文地址:https://www.cnblogs.com/daxiong225/p/8961081.html
Copyright © 2020-2023  润新知