Guava-File文件操做

guava-file文件操做java

今天看guava的file時看到了一個功能是建立臨時文件夾,因而就多看了一些其餘的關於file的操做API。ide

一. Guava的文件寫入google

Guava的Files類中提供了幾個write方法來簡化向文件中寫入內容的操做,下面的例子演示 Files.write(byte[],File)的用法。code

import static com.google.common.base.Preconditions.*;
import static java.lang.System.*;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
//此處省略class
/**
* 演示向文件中寫入字節流
* 
* @param fileName 要寫入文件的文件名
* @param contents 要寫入的文件內容
*/
public void demoFileWrite(final String fileName, final String contents)
{
checkNotNull(fileName, "Provided file name for writing must NOT be null.");
checkNotNull(contents, "Unable to write null contents.");
final File newFile = new File(fileName);
try
{
Files.write(contents.getBytes(), newFile);
}
catch (IOException fileIoEx)
{
err.println(  "ERROR trying to write to file '" + fileName + "' - "
+ fileIoEx.toString());
}
}

二.得到文件內容接口

Files類提供了readLines方法能夠方便的讀取文件的內容,以下demo代碼:內存

String testFilePath = "d:\\test.txt";
File testFile = new File(testFilePath);
List<String> lines = Files.readLines(testFile, Charsets.UTF_16);
for (String line : lines) {
System.out.println(line);
}

注意這裏的readLines方法返回的是List\<String>的接口,這對於大文件處理是會有問題的。大文件處理能夠使用readLines方法的另外一個重載。下面的例子演示從一個大文件中逐行讀取文本,並作行號計數。get

package main.com.jd.coo.guava.io;
import java.io.File;
import java.io.IOException;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;
/**
* Created by outofmemory.cn  on 2014/7/24.
*/
public class FilesDemo {
static class CounterLine implements LineProcessor<Integer> {
private int rowNum = 0;
@Override
public boolean processLine(String line) throws IOException {
rowNum ++;
return true;
}
@Override
public Integer getResult() {
return rowNum;
}
}
public static void main(String[] args) throws IOException {
String testFilePath = "d:\\test.txt";
File testFile = new File(testFilePath);
CounterLine counter = new CounterLine();
Files.readLines(testFile, Charsets.UTF_16, counter);
System.out.println(counter.getResult());
}
}

這個readLines的重載,須要咱們實現一個LineProcessor的泛型接口,在這個接口的實現方法processLine方法中咱們能夠對行文本進行處理,getResult方法能夠得到一個最終的處理結果,這裏咱們只是簡單的返回了一個行計數。hash

另外還有readBytes方法能夠對文件的字節作處理,readFirstLine能夠返回第一行的文本,Files.toString(File,Charset)能夠返回文件的全部文本內容。it

三. 複製移動(剪切)文件io

在Guava中複製文件操做提供了一組的copy方法,咱們看一個示例:

/**
* 演示如何使用guava的Files.copy方法複製文件
* 
* @param sourceFileName 複製的源文件名
* @param targetFileName 目標文件名
*/
public void demoSimpleFileCopy(
final String sourceFileName, final String targetFileName)
{
checkNotNull(sourceFileName, "Copy source file name must NOT be null.");
checkNotNull(targetFileName, "Copy target file name must NOT be null.");
final File sourceFile = new File(sourceFileName);
final File targetFile = new File(targetFileName);
try
{
Files.copy(sourceFile, targetFile);
}
catch (IOException fileIoEx)
{
err.println(
"ERROR trying to copy file '" + sourceFileName
+ "' to file '" + targetFileName + "' - " + fileIoEx.toString());
}
}

Guava中移動文件使用move方法,用法和copy同樣。

四. 比較文件內容

Guava中提供了Files.equal(File,File)方法來比較兩個文件的內容是否徹底一致,請看下面的示例:

/**
* 演示 Files.equal(File,File) 來比較兩個文件的內容
* 
* @param fileName1 比較的文件1文件名
* @param fileName2 比較的文件2文件名
*/
public void demoEqual(final String fileName1, final String fileName2)
{
checkNotNull(fileName1, "First file name for comparison must NOT be null.");
checkNotNull(fileName2, "Second file name for comparison must NOT be null.");
final File file1 = new File(fileName1);
final File file2 = new File(fileName2);
try
{
out.println(
"File '" + fileName1 + "' "
+ (Files.equal(file1, file2) ? "IS" : "is NOT")
+ " the same as file '" + fileName2 + "'.");
}
catch (IOException fileIoEx)
{
err.println(
"ERROR trying to compare two files '"
+ fileName1 + "' and '" + fileName2 + "' - " + fileIoEx.toString());
}
}

五. 其餘有用的方法

Guava的Files類中還提供了其餘一些文件的簡捷方法。好比

  1. touch方法建立或者更新文件的時間戳。
  2. createTempDir()方法建立臨時目錄
  3. Files.createParentDirs(File) 建立父級目錄
  4. getChecksum(File)得到文件的checksum
  5. hash(File)得到文件的hash
  6. map系列方法得到文件的內存映射
  7. getFileExtension(String)得到文件的擴展名
  8. getNameWithoutExtension(String file)得到不帶擴展名的文件名
  9. Guava的方法都提供了一些重載,這些重載能夠擴展基本用法,咱們也有必要去多瞭解一下,這些重載的方法。
相關文章
相關標籤/搜索