StreamTool 字节流保存,字节流读取下一行,读取流
来源:互联网 发布:淘宝乐高军事玩具大全 编辑:程序博客网 时间:2024/06/11 01:36
StreamTool 字节流保存,字节流读取下一行,读取流
在读取流的过程中,有时会不确定是否是字节流或者是字符流,统一用字节流读取,然后对流进行处理:
public class StreamTool { /** * 将字节流保存到文件中 * @param file * @param data * @throws Exception */ public static void save(File file, byte[] data) throws Exception { FileOutputStream outStream = new FileOutputStream(file); outStream.write(data); outStream.close(); } /** * 读取下一行 * @param in * @return String * @throws IOException */ public static String readLine(PushbackInputStream in) throws IOException { char buf[] = new char[128]; int room = buf.length; int offset = 0; int c; loop: while (true) { switch (c = in.read()) { case -1: case '\n': break loop; case '\r': int c2 = in.read(); if ((c2 != '\n') && (c2 != -1)) in.unread(c2); break loop; default: if (--room < 0) { char[] lineBuffer = buf; buf = new char[offset + 128]; room = buf.length - offset - 1; System.arraycopy(lineBuffer, 0, buf, 0, offset); } buf[offset++] = (char) c; break; } } if ((c == -1) && (offset == 0)) return null; return String.copyValueOf(buf, 0, offset); } /** * 读取流 * @param inStream * @return 字节数组 * @throws Exception */ public static byte[] readStream(InputStream inStream) throws Exception{ ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while( (len=inStream.read(buffer)) != -1){ outSteam.write(buffer, 0, len); } outSteam.close(); inStream.close(); return outSteam.toByteArray(); }}
0 0
- StreamTool 字节流保存,字节流读取下一行,读取流
- 字节流读取文件
- 文件读取字节流
- C# 学习: 读取文件流,保存到字节中
- 使用字节流、客户端读取图片发送至服务端保存
- C#读取二进制字节流
- C#读取二进制字节流
- 模拟字节读取流缓冲区
- Java IO流字节读取
- 字节流读取数据注意事项
- 字节流_FileInputStream类读取
- java 字节流读取图片,字符流读取,二进制读取
- java 字节流读取图片,字符流读取,二进制读取
- java 字节流读取图片,字符流读取,二进制读取
- java 字节流读取图片,字符流读取,二进制读取
- java 字节流读取图片,字符流读取,二进制读取
- 通过java字节流复制文件,逐个字节读取写入
- java缓冲字节流复制文件,逐个字节读取、写入
- DBI(i80)/DPI(RGB)/DSI
- java学习 十五、面向对象
- Extjs监听浏览器刷新、关闭和窗口关闭功能
- 最短路练习10/poj/1511 Invitation Cards ,(两次spfa),(单源最短路,优先队列优化的Dijkstra)
- Unity脚本引用原理,修复Unity脚本引用丢失,源码脚本与dll中的脚本引用互换 .
- StreamTool 字节流保存,字节流读取下一行,读取流
- grep
- svn certificate validation failed解决办法
- 最长公共子序列问题·
- 跳表实现
- 1758: [Wc2010]重建计划(TLE)
- Unity3D 自动添加Fbx Animation Event
- 欢迎使用CSDN-markdown编辑器
- 一个java页游服务器框架