码迷,mamicode.com
首页 > 其他好文 > 详细

Files

时间:2016-08-17 01:19:32      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

guava源码:Files

看一下它的调用过程

 public static <T> T readLines(File file, Charset charset,
    LineProcessor<T> callback) throws IOException {
  return asCharSource(file, charset).readLines(callback);
}

  asCharSource->asByteSource->FileByteSource

FileByteSource里 是用的FIleInputStream

 @Override
public FileInputStream openStream() throws IOException {
  return new FileInputStream(file);
}

在asByteSource里 对FIleByteSource转换 成BufferedReader

 public static BufferedReader newReader(File file, Charset charset)
    throws FileNotFoundException {
  checkNotNull(file);
  checkNotNull(charset);
  return new BufferedReader(
      new InputStreamReader(new FileInputStream(file), charset));
}

在ReadLines中

 public static <T> T readLines(
    Readable readable, LineProcessor<T> processor) throws IOException {
  checkNotNull(readable);
  checkNotNull(processor);

  LineReader lineReader = new LineReader(readable);
  String line;
  while ((line = lineReader.readLine()) != null) {
    if (!processor.processLine(line)) {
      break;
    }
  }
  return processor.getResult();
}

Files

标签:

原文地址:http://www.cnblogs.com/lijia0511/p/5778307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!