Android笔记
我的博客
Android笔记
Android笔记
  • Android学习资料
  • Android知识点梳理
  • Android Studio
  • aop
  • UI
    • ViewPager2
    • View
      • View.post()分析
      • 自定义View
        • Canvas使用
        • Paint使用
        • Path使用
        • PathMeasure使用
        • Matrix使用
      • View事件分发
    • TextView
    • EditText
    • 引导页
    • 菜单
    • Toolbar
    • RecyclerView
    • ViewPager
    • BottomSheet
    • 通知
    • CalendarView
    • TimePicker
    • LayoutInfalter
    • shadowlayout
    • 状态栏适配
    • RecyclerView
    • 屏幕适配
  • 组件
    • Activity
    • Intent
    • 服务
      • 服务
      • 绑定服务
      • AIDL
    • Broadcasts
    • Fragment
    • ContentProvider
  • 权限
  • 数据存储
    • 数据和文件存储
    • 将文件保存到外部存储中
    • 将文件保存在内部存储中
    • Android 存储用例和最佳做法
    • 保存键值对数据
  • 编译时注解
    • Untitled
    • Java注解处理器](annotation/annotation-processing.md)
    • JavaPoet 文档翻译
  • 图形和图像
    • Drawable使用
  • 网络
    • WebView
    • OkHttp文档翻译
      • OkHttp使用
      • OkHttp拦截器
    • Retrofit使用
    • Retrofit源码分析
    • HttpURLConnection使用
    • Volley使用
    • Volley源码分析
    • Glide使用
    • Glide源码分析
    • RxJava
  • 插件化
    • 插件化框架
    • VirtualAPK分析
  • 依赖注入
  • 开源库
    • EventBus源码分析
    • Okio概览
    • Okio菜谱
    • Okio
    • OkHttp拦截器
    • OKHttp缓存
    • Okhttp源码分析
  • 集合
    • SparseArray
    • ArrayMap
  • 架构
  • Jetpack
    • databinding adapter
    • databinding坑
    • databinding源码分析
    • Android Data Binding: Let’s Flip This Thing
    • Untitled
    • Lifecycle源码分析
    • ViewModel源码分析
    • LiveData源码分析
  • 音频和视频
    • CameraX使用
    • ExoPlayer
    • GsyVideoPlayer
  • 性能优化
    • 崩溃优化
    • 内存优化
      • LeakCanary如何工作
    • 渲染优化
    • 电池优化
    • 启动优化
    • 网络优化
    • 安装包大小优化
  • NDK开发
  • 系统源码分析
    • Android源码下载
    • Android系统启动
      • init进程启动过程
      • Zygote进程启动过程
      • Zygote进程启动流程
      • SystemServer处理过程
    • 应用程序进程启动过程
      • 应用程序进程启动过程介绍
    • Window
    • WMS
    • WindowManagerService
    • 四大组件的工作过程
      • Activity启动流程分析
      • Activity启动流程
    • ActivityManagerService分析
    • Context详解
    • Binder原理
      • Binder驱动
      • 获取ServiceManager
      • 启动ServiceManager
      • Parcel源码分析
    • Android图形系统概述
    • Choreographer原理
    • Handler使用
    • Handler源码分析
  • 逆向
    • Android抓包
    • 网易云音乐逆向
由 GitBook 提供支持
在本页
  • 逐行读取一个文本文件
  • 写入一个文本文件
在GitHub上编辑
  1. 开源库

Okio菜谱

???+ 原文

We’ve written some recipes that demonstrate how to solve common problems with Okio. Read through them to learn about how everything works together. Cut-and-paste these examples freely; that’s what they’re for.

我们写了一些菜谱,演示如何用Okio解决常见问题。阅读它们,了解一切是如何运作的。自由地剪切和粘贴这些例子;这就是它们的用处。

???+ 原文

These recipes work on all platforms: Java, Android, Kotlin/Native, and Kotlin/JS. See [java.io Recipes](https://square.github.io/okio/java_io_recipes/) for samples that integrate Java APIs.

这些配方可以在所有平台上使用。Java、Android、Kotlin/Native和Kotlin/JS。请参阅java.io Recipes,了解集成了Java APIs的样本。

逐行读取一个文本文件

???+ 原文

Use FileSystem.source(Path) to open a source stream to read a file. The returned Source interface is very small and has limited uses. Instead we wrap the source with a buffer. This has two benefits:

使用FileSystem.source(Path)打开一个源流以读取文件。返回的Source接口非常小,用途有限。相反,我们用一个缓冲区来包装源。这有两个好处。

???+ 原文

- **It makes the API more powerful.** Instead of the basic methods offered by `Source`, `BufferedSource` has dozens of methods to address most common problems concisely.
- **It makes your program run faster.** Buffering allows Okio to get more done with fewer I/O operations.
  • 它使API更加强大。与Source提供的基本方法不同,BufferedSource有几十种方法可以简洁地解决大多数常见问题。

  • 它让你的程序运行得更快。缓冲使Okio可以用较少的I/O操作完成更多的工作。

???+ 原文

Each `Source` that is opened needs to be closed. The code that opens the stream is responsible for making sure it is closed.

每个被打开的Source都需要被关闭。打开流的代码要负责确保它被关闭。

public void readLines(Path path) throws IOException {
  try (Source fileSource = FileSystem.SYSTEM.source(path);
       BufferedSource bufferedFileSource = Okio.buffer(fileSource)) {

    while (true) {
      String line = bufferedFileSource.readUtf8Line();
      if (line == null) break;

      if (line.contains("square")) {
        System.out.println(line);
      }
    }

  }
}

???+ 原文

The `readUtf8Line()` API reads all of the data until the next line delimiter – either `\n`, `\r\n`, or the end of the file. It returns that data as a string, omitting the delimiter at the end. When it encounters empty lines the method will return an empty string. If there isn’t any more data to read it will return null.

readUtf8Line()API读取了所有的数据,直到下一个分界符--或者是 \n, \r\n, 或者是文件的结尾。它以字符串的形式返回数据,省略最后的分界符。当它遇到空行时,该方法将返回一个空字符串。如果没有更多的数据可以读取,它将返回null。

public void readLines(Path path) throws IOException {
  try (BufferedSource source = Okio.buffer(FileSystem.SYSTEM.source(path))) {
    for (String line; (line = source.readUtf8Line()) != null; ) {
      if (line.contains("square")) {
        System.out.println(line);
      }
    }
  }
}

???+ 原文

The `readUtf8Line()` method is suitable for parsing most files. For certain use-cases you may also consider `readUtf8LineStrict()`. It is similar but it requires that each line is terminated by `\n` or `\r\n`. If it encounters the end of the file before that it will throw an `EOFException`. The strict variant also permits a byte limit to defend against malformed input.

readUtf8Line()方法适用于解析大多数文件。对于某些使用情况,你也可以考虑readUtf8LineStrict()。它与此类似,但它要求每一行以 \n 或 \r\n 结尾。如果它在这之前遇到了文件的结尾,它将抛出一个EOFException。严格的变体还允许有一个字节限制,以防御畸形的输入。

写入一个文本文件

???+ 原文

Above we used a `Source` and a `BufferedSource` to read a file. To write, we use a `Sink` and a `BufferedSink`. The advantages of buffering are the same: a more capable API and better performance.

上面我们用一个Source和一个BufferedSource来读一个文件。写入时,我们使用一个Sink和一个BufferedSink。缓冲的优点是一样的:一个更有能力的API和更好的性能。

public void writeEnv(Path path) throws IOException {
  try (Sink fileSink = FileSystem.SYSTEM.sink(path);
       BufferedSink bufferedSink = Okio.buffer(fileSink)) {

    for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
      bufferedSink.writeUtf8(entry.getKey());
      bufferedSink.writeUtf8("=");
      bufferedSink.writeUtf8(entry.getValue());
      bufferedSink.writeUtf8("\n");
    }

  }
}

???+ 原文

There isn’t an API to write a line of input; instead we manually insert our own newline character. Most programs should hardcode `"\n"` as the newline character. In rare situations you may use `System.lineSeparator()` instead of `"\n"`: it returns `"\r\n"` on Windows and `"\n"` everywhere else.

没有一个API来写输入行,而是我们手动插入我们自己的换行符。大多数程序应该硬编码"\n "作为换行符。在极少数情况下,你可以使用System.lineSeparator()代替"\n":它在Windows上返回"\r\n",在其他地方返回"\n"。

public void writeEnv(Path path) throws IOException {
  try (BufferedSink sink = Okio.buffer(FileSystem.SYSTEM.sink(path))) {
    for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
      sink.writeUtf8(entry.getKey())
        .writeUtf8("=")
        .writeUtf8(entry.getValue())
        .writeUtf8("\n");
    }
  }
}
上一页Okio概览下一页Okio

最后更新于3年前