package guavaIO;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import static com.google.common.base.Preconditions.*;
import com.google.common.base.Charsets;
import com.google.common.io.LineProcessor;
import org.apache.commons.collections.CollectionUtils;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* User: zhaoyun
* Date: 16-5-18
* Time: 下午2:15
* Guava I/O
*/
public class GuavaIO {
private static final String FILEPATH = "d:/GuavaIO.txt";
private static final String CONTENTS = "城市套路玩的深,我要尽快回农村。";
private static final String TARGETFILE = "d:/GuavaIO_First.txt";
public static void main(String[] args) throws IOException{
//文件写入
fileWrite(FILEPATH,CONTENTS);
//读文件
List<String> list = readFileContents(FILEPATH);
for(String str : list){
System.out.println(str);
}
//大文件读
long startTime = System.currentTimeMillis();
readBigFile(FILEPATH); //todo 测试读取500000行数据的文本 耗时489ms
System.out.println("Use Time:" + (System.currentTimeMillis()-startTime));
//文件的复制 移动 剪切
fileCopy(FILEPATH,TARGETFILE);
//移动文件
moveFile(FILEPATH,TARGETFILE);
//文件比较
equalFile(FILEPATH,TARGETFILE);
}
/**
* Write File
* @param fileName
* @param contents
*/
public static void fileWrite(final String fileName,final String contents){
checkNotNull(fileName,"error: fileName is null");
checkNotNull(contents,"error: contents is null");
final File newFile = new File(fileName);
try{
Files.write(contents.getBytes(),newFile);
}catch (Exception e){
System.out.println("write file error:" + e.getMessage());
}
}
/**
* Read File
* @param filePath
* @return
*/
public static List<String> readFileContents(String filePath){
List<String> list = Lists.newArrayList();
checkNotNull(filePath,"error: filePath is null");
File file = new File(filePath);
try{
list = Files.readLines(file, Charsets.UTF_8); // readLines返回的是集合 对于大文件处理不适合
}catch (Exception e){
System.out.println("read file error:" + e.getMessage());
}
return list;
}
/**
* Read Big File Method
* @param filePath
*/
public static void readBigFile(String filePath) throws IOException{
checkNotNull(filePath,"error: filePath is null");
File file = new File(filePath);
CounterLine counterLine = new CounterLine();
Files.readLines(file, Charsets.UTF_8,counterLine);
// Map<Integer,String> map = counterLine.getResult();
// if(map != null){
// Set<Map.Entry<Integer,String>> entrySet = map.entrySet();
// for(Map.Entry entry : entrySet){
// System.out.println("Key:" + entry.getKey() + " Value:" + entry.getValue());
// }
// }
}
static class CounterLine implements LineProcessor<Map<Integer,String>>{
private Map<Integer,String> map = Maps.newHashMap();
private int rowNum = 0;
@Override
public boolean processLine(String line) throws IOException {
rowNum++;
map.put(rowNum,line);
return true;
}
@Override
public Map<Integer,String> getResult() {
return map;
}
}
/**
* File Copy
* @param sourceFileName
* @param targetFileName
*/
public static void fileCopy(final String sourceFileName,final String targetFileName){
checkNotNull(sourceFileName,"error: sourceFile is null");
checkNotNull(targetFileName,"error: targetFile is null");
final File sourceFile = new File(sourceFileName);
final File targetFile = new File(targetFileName);
try{
Files.copy(sourceFile,targetFile);
}catch (Exception e){
System.out.println("error: copy file " + e.getMessage());
}
}
/**
* File Move
* @param sourceFileName
* @param targetFileName
*/
public static void moveFile(final String sourceFileName,final String targetFileName){
final File sourceFile = new File(sourceFileName);
final File targetFile = new File(targetFileName);
try{
Files.move(sourceFile,targetFile);
}catch (Exception e){
System.out.println("error: copy file " + e.getMessage());
}
}
/**
* 文件比较
* @param sourceFileName
* @param targetFileName
*/
public static void equalFile(final String sourceFileName,final String targetFileName) throws IOException{
checkNotNull(sourceFileName,"error: sourceFile is null");
checkNotNull(targetFileName,"error: targetFile is null");
final File sourceFile = new File(sourceFileName);
final File targetFile = new File(targetFileName);
System.out.println("File equal:" + Files.equal(sourceFile,targetFile));
}
}