Okio
Okio
- ByteString:不可变的字节序列,类似
String 类,使用它更容易处理二进制数据。 - Buffer:可变的字节序列,类似
ArrayList 。 - Source:类似
InputStream( 输入流) - Sink:类似
OutputStream( 输出流)
Source
try {
File file = new File("test.txt");
BufferedSource source = Okio.buffer(Okio.source(file));
//byte[] data = source.readByteArray();
//System.out.println(new String(data, Charset.forName("UTF-8")));
BufferedSink sink = Okio.buffer(Okio.sink(new File("test2.txt")));
source.readAll(sink);
sink.close();
source.close();
} catch (IOException e) {
e.printStackTrace();
}
Sink
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();
}