二、使用Okio框架进行输出操作(Sink)
2015-03-20 18:20
106 查看
Sink相当于输出流(OutputStream),把内存中的内容输出到硬盘。
![](http://s3.51cto.com/wyfs02/M02/5B/94/wKiom1UL7ifA8Gq9AABmy7lI3Yo667.jpg)
关于Flushable、Closeable 、AutoCloseable接口的说明,请参考以下文档:
探究java IO之AutoCloseable,Closeable和Flushable接口
例子:
BufferedSink API
本文出自 “我的编程之路” 博客,请务必保留此出处http://aiwoapp.blog.51cto.com/8677066/1622635
![](http://s3.51cto.com/wyfs02/M02/5B/94/wKiom1UL7ifA8Gq9AABmy7lI3Yo667.jpg)
关于Flushable、Closeable 、AutoCloseable接口的说明,请参考以下文档:
探究java IO之AutoCloseable,Closeable和Flushable接口
例子:
try { File file = new File("test.txt"); //如果文件不存在,则自动创建 BufferedSink sink = Okio.buffer(Okio.sink(file)); sink.writeUtf8("Hello, World"); sink.writeString("测试信息", Charset.forName("UTF-8")); sink.close(); } catch (IOException e) { e.printStackTrace(); }
BufferedSink API
/** * 返回Sink内部的Buffer. */ Buffer buffer(); /** * 写ByteString中的数据到Sink */ BufferedSink write(ByteString byteString) throws IOException; /** * 写字节数组到Sink。类似OutputStream#write(byte[]) */ BufferedSink write(byte[] source) throws IOException; /** * 将指定 byte 数组中从偏移量off开始的len个字节写入Sink。类似 * OutputStream#write(byte[], int, int) */ BufferedSink write(byte[] source, int offset, int byteCount) throws IOException; /** * 从Source中移除所有的字节,把他们写入到Sink中,返回读取的字节数。如果Source中 * 的内容已被读取,则返回0。 */ long writeAll(Source source) throws IOException; /** * 从Source中移除byteCount字节,并把他们写入到Sink中 */ BufferedSink write(Source source, long byteCount) throws IOException; /** * 以UTF-8编码把内容写入到Sink中 */ BufferedSink writeUtf8(String string) throws IOException; /** * 以charset编码把内容写入到Sink中 */ BufferedSink writeString(String string, Charset charset) throws IOException; /** * Writes a byte to this sink. */ BufferedSink writeByte(int b) throws IOException; /** * Writes a big-endian short to this sink using two bytes. */ BufferedSink writeShort(int s) throws IOException; /** * Writes a little-endian short to this sink using two bytes. */ BufferedSink writeShortLe(int s) throws IOException; /** * Writes a big-endian int to this sink using four bytes. */ BufferedSink writeInt(int i) throws IOException; /** * Writes a little-endian int to this sink using four bytes. */ BufferedSink writeIntLe(int i) throws IOException; /** * Writes a big-endian long to this sink using eight bytes. */ BufferedSink writeLong(long v) throws IOException; /** * Writes a little-endian long to this sink using eight bytes. */ BufferedSink writeLongLe(long v) throws IOException; /** * Writes a long to this sink in signed decimal form (i.e., as a string in base 10). */ BufferedSink writeDecimalLong(long v) throws IOException; /** * Writes a long to this sink in hexadecimal form (i.e., as a string in base 16). */ BufferedSink writeHexadecimalUnsignedLong(long v) throws IOException; /** * Writes complete segments to the underlying sink, if one exists. Like {@link #flush}, but * weaker. Use this to limit the memory held in the buffer to a single segment. */ BufferedSink emitCompleteSegments() throws IOException; /** * Writes all buffered data to the underlying sink, if one exists. Like {@link #flush}, but * weaker. Call this before this buffered sink goes out of scope so that its data can reach its * destination. */ BufferedSink emit() throws IOException; /** * 返回Sink中的输出流. */ OutputStream outputStream();
本文出自 “我的编程之路” 博客,请务必保留此出处http://aiwoapp.blog.51cto.com/8677066/1622635
相关文章推荐
- 三、使用Okio框架进行输入操作(Source)
- 简单的三层框架以及使用dbutils进行数据库操作(入门)
- 使用DBUtil框架进行多表操作
- day03-spring与数据库操作的框架解析即dataSource的获取(JDBC)、使用spring中已经有的事务进行JDBC操作
- 如何在C++中使用cout进行高级的格式化输出操作
- 【hibernate框架】使用Annotation注解进行实体类操作
- 简单的三层框架以及使用dbutils进行数据库操作(入门)
- Web自动化框架LazyUI使用手册(6)--8种控件对应的class,及可对其进行的操作
- 使用dom4j对xml文件进行读取和输出操作
- 简单使用dom4j对xml文件进行输出操作(如何使用dom4j对xml文件进行输出)
- 在EXT框架中,使用JS文件设置UEditor文本框,出现新增内容很多,页面变型,不出现滚动条,导致无法进行操作。
- 使用MyBatis框架进行数据库的增删改查操作
- 简单的三层框架以及使用dbutils进行数据库操作(入门)
- 使用iOS原生sqlite3框架对sqlite数据库进行操作
- 使用iOS原生sqlite3框架对sqlite数据库进行操作
- 在Java中使用标准输入输出设备进行字符串,整数浮点数等 的输入输出操作
- 简单的三层框架以及使用dbutils进行数据库操作(入门)
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- Web自动化框架LazyUI使用手册(6)--8种控件对应的class,及可对其进行的操作