/** * 将文本输入流写入一个文本输出流。块大小为 8192 * * <p><b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer 输出流 * @param reader 输入流 */ public static void writeAndClose(Writer writer, Reader reader) { try { write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(writer); safeClose(reader); } }
/** * 将输入流写入一个输出流。块大小为 8192 * * <p><b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops 输出流 * @param ins 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(ops); safeClose(ins); } }
/** * 将一段文本全部写入一个writer。 * * <p><b style=color:red>注意</b>,它会关闭输出流 * * @param writer 输出流 * @param cs 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(writer); } }
/** * 从一个文本流中读取全部内容并返回 * * <p><b style=color:red>注意</b>,它会关闭输入流 * * @param reader 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } }
/** * 将一个字节数组写入一个输出流。 * * <p><b style=color:red>注意</b>,它会关闭输出流 * * @param ops 输出流 * @param bytes 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(ops); } }
/** * 从一个流读取数据库配置 * * @param in 输入流,包含配置信息 * @throws IOException 读取失败是抛出异常 */ public void init(InputStream in) throws IOException { Properties props = new Properties(); try { props.load(in); init(props); } finally { Streams.safeClose(in); } }
/** * 将一段字符串写入一个文本输出流,并将该流关闭 * * @param writer 文本输出流 * @param str 字符串 */ public static void writeAll(Writer writer, String str) { try { writer.write(str); writer.flush(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(writer); } }
public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } }
/** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; }
// 判断编译等级 public static boolean isJDK6() { InputStream is = null; try { String classFileName = "/" + Lang.class.getName().replace('.', '/') + ".class"; is = Lang.class.getClassLoader().getResourceAsStream(classFileName); if (is != null && is.available() > 8) { is.skip(7); return is.read() > 49; } } catch (Throwable e) { } finally { Streams.safeClose(is); } return false; }
public static void loadProperties(String fileName) { InputStream is = null; try { pp = new Properties(); File f = Files.findFile(fileName); if (f == null) throw new RuntimeException("nutz-test.properties Not FOUND!!!"); is = Streams.fileIn(f); pp.load(is); pp.list(System.out); } catch (Exception e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(is); } }
/** * 从一个文本输入流读取所有内容,并将该流关闭 * * @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 Lang.wrapThrow(e); } finally { Streams.safeClose(reader); } }