zoukankan      html  css  js  c++  java
  • Guava源码阅读ioFiles

    package com.google.common.io;

    今天阅读一个非常常用的类Files,文件操作类。

     


     

    readLines(File file, Charset charset),这个方法将File按行存入list<String>中。
    return readLines(
    file,
    charset, //编码方式,通常都是utf-8
    new LineProcessor<List<String>>() {
    final List<String> result = Lists.newArrayList();

    @Override
    public boolean processLine(String line) {
    result.add(line);
    return true;
    }

    @Override
    public List<String> getResult() {
    return result;
    }
    });

     
    public static <T> T readLines(File file, Charset charset, LineProcessor<T> callback)  //这个方法在将文件按行存储为Lists<String>时,同时调用行处理方法,只有满足要求的结果才会存到结果中。
    throws IOException {
    return asCharSource(file, charset).readLines(callback);
    }

     
    public static String toString(File file, Charset charset) throws IOException { //将文件转化为String,并返回,包含文件中的所有字符,包括换行符
    return asCharSource(file, charset).read();
    }

     
    public static boolean equal(File file1, File file2) throws IOException //如果这两个文件相同,则返回true。这时不仅是内容,还包括文件长度。
    
    
    if (file1 == file2 || file1.equals(file2)) {
    return true;
    }
    
    
    if (len1 != 0 && len2 != 0 && len1 != len2) {
    return false;
    }

     
    public static void copy(File from, File to) throws IOException { //拷贝一个文件里的所以字符给另一个文件
    checkArgument(!from.equals(to), "Source %s and destination %s must be different", from, to);
    asByteSource(from).copyTo(asByteSink(to));
    }

     
    public static void write(CharSequence from, File to, Charset charset) throws IOException {
    
    
    //将指定内容写入文件,如果文件原本存在内容,则覆盖
    asCharSink(to, charset).write(from);
    }


     
    
    
    public static void append(CharSequence from, File to, Charset charset) throws IOException { //追加文件,将指定内容追加到文件尾
    write(from, to, charset, true);
    }
     
     
     
     
     
     
     
     
  • 相关阅读:
    bzoj3237[Ahoi2013] 连通图
    bzoj3075[Usaco2013]Necklace
    bzoj1876[SDOI2009] SuperGCD
    bzoj3295[Cqoi2011] 动态逆序对
    BestCoder#86 E / hdu5808 Price List Strike Back
    bzoj2223[Coci 2009] PATULJCI
    bzoj2738 矩阵乘法
    poj 1321 -- 棋盘问题
    poj 3083 -- Children of the Candy Corn
    poj 2488 -- A Knight's Journey
  • 原文地址:https://www.cnblogs.com/haolnu/p/7359489.html
Copyright © 2011-2022 走看看