示例#1
0
 /**
  * 将一段字符串写入一个文本输出流,并将该流关闭
  *
  * @param writer 文本输出流
  * @param str 字符串
  */
 public static void writeAll(Writer writer, String str) {
   try {
     writer.write(str);
     writer.flush();
   } catch (IOException e) {
     throw LE.wrapThrow(e);
   } finally {
     IOUtils.close(writer);
   }
 }
示例#2
0
  /**
   * 从一个文本输入流读取所有内容,并将该流关闭
   *
   * @param reader 文本输入流
   * @return 输入流所有内容
   */
  public static String readAll(Reader reader) {
    if (!(reader instanceof BufferedReader)) reader = new BufferedReader(reader);
    try {
      StringBuilder sb = new StringBuilder();

      char[] data = new char[64];
      int len;
      while (true) {
        if ((len = reader.read(data)) == -1) break;
        sb.append(data, 0, len);
      }
      return sb.toString();
    } catch (IOException e) {
      throw LE.wrapThrow(e);
    } finally {
      IOUtils.close(reader);
    }
  }