關於IO操作Guava給我們提供了很多工具類,大大提高了我們開發效率.下面我們將對Guava IO 提供的相關工具類做一個簡單的介紹.涉及到的工具類主要有:ByteStreams,CharStreams,Resources,Closeables,Flushables,Files,MoreFiles.
一 ByteStreams
ByteStreams裏面提供用於處理字節數組和I / O流的實用程序方法。
1.1 ByteStreams常用方法
public final class ByteStreams {
/**
* 拷貝從from到to
*/
@CanIgnoreReturnValue
public static long copy(InputStream from, OutputStream to) throws IOException;
/**
* 拷貝從from到to
*/
@CanIgnoreReturnValue
public static long copy(ReadableByteChannel from, WritableByteChannel to) throws IOException;
/**
* InputStream裏面的數據讀到byte數組裏面去
*/
public static byte[] toByteArray(InputStream in) throws IOException;
/**
*
* 從給定的InputStream讀取並丟棄數據,直到到達流的末尾。返回讀取的總字節數。不關閉流
*/
@CanIgnoreReturnValue
@Beta
public static long exhaust(InputStream in) throws IOException;
/**
* byte數組裏面的數據導入到ByteArrayDataInput裏面去
*/
@Beta
public static ByteArrayDataInput newDataInput(byte[] bytes);
/**
* byte數組裏面的數據導入到ByteArrayDataInput裏面去
*/
@Beta
public static ByteArrayDataInput newDataInput(byte[] bytes, int start);
/**
* ByteArrayInputStream裏面的數據導入到ByteArrayDataInput裏面去
*/
@Beta
public static ByteArrayDataInput newDataInput(ByteArrayInputStream byteArrayInputStream);
/** 返回ByteArrayDataOutput,默認size = 32 */
@Beta
public static ByteArrayDataOutput newDataOutput() {
return newDataOutput(new ByteArrayOutputStream());
}
/**
* 返回ByteArrayDataOutput,給定了初始數組大小size
*/
@Beta
public static ByteArrayDataOutput newDataOutput(int size);
/**
* 返回ByteArrayDataOutput,並且把ByteArrayOutputStream裏面的數據導入進去
*/
@Beta
public static ByteArrayDataOutput newDataOutput(ByteArrayOutputStream byteArrayOutputSteam);
/**
* 返回一個空的OutputStream,裏面什麼數據也沒有
*/
@Beta
public static OutputStream nullOutputStream();
/**
* 重新包裝下InputStream,限制可讀的字節數
*/
@Beta
public static InputStream limit(InputStream in, long limit);
/**
* InputStream裏面的數據讀到byte數組裏面去。如果讀到數據的長度和給定的數組長度不相同拋EOFException異常
*/
@Beta
public static void readFully(InputStream in, byte[] b) throws IOException;
/**
* InputStream裏面的數據讀到byte數組裏面去,如果讀到數據的長度和給定的len長度不相同拋EOFException異常
*/
@Beta
public static void readFully(InputStream in, byte[] b, int off, int len) throws IOException;
/**
* InputStream裏面丟棄n個字節的數據
*/
@Beta
public static void skipFully(InputStream in, long n) throws IOException;
/**
* 把InputStream裏面的數據用ByteProcessor來處理
*/
@Beta
@CanIgnoreReturnValue // some processors won't return a useful result
public static <T> T readBytes(InputStream input, ByteProcessor<T> processor);
/**
* 把InputStream裏面的數據讀到byte數組裏面去
*/
@Beta
@CanIgnoreReturnValue
// Sometimes you don't care how many bytes you actually read, I guess.
// (You know that it's either going to read len bytes or stop at EOF.)
public static int read(InputStream in, byte[] b, int off, int len);
}
1.2 ByteStreams簡單使用
// ByteStreams.copy()方法,數據拷貝
@Test
public void copy() {
URL url = Resources.getResource("application.yml");
File f = new File(url.getFile()); // 聲明File對象
InputStream inputStream = null;
try {
inputStream = new FileInputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (inputStream == null) {
return;
}
try {
OutputStream outputStream = new FileOutputStream("/home/tuacy/github/google-guava-study/src/main/resources" + File.separator + "abc.txt");
// 把InputStream裏面的內容寫入到OutputStream裏面去
ByteStreams.copy(inputStream, outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
// ByteStreams.toByteArray()方法,把InputStream裏面的數據讀到數組裏面去
@Test
public void toByteArray() {
URL url = Resources.getResource("application.yml");
File f = new File(url.getFile()); // 聲明File對象
// InputStream
InputStream inputStream = null;
try {
inputStream = new FileInputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (inputStream == null) {
return;
}
try {
// InputStream裏面的內容讀到byte數組裏面去
byte[] byteArrary = ByteStreams.toByteArray(inputStream);
System.out.println(new String(byteArrary));
} catch (IOException e) {
e.printStackTrace();
}
}
// ByteStreams.read() 把
@Test
public void read() {
URL url = Resources.getResource("application.yml");
File f = new File(url.getFile()); // 聲明File對象
// InputStream
InputStream inputStream = null;
try {
inputStream = new FileInputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (inputStream == null) {
return;
}
try {
byte[] byteArray = new byte[1024];
int readLength = ByteStreams.read(inputStream, byteArray, 0, 1024);
System.out.println("讀取都的數據長度 = " + readLength);
} catch (IOException e) {
e.printStackTrace();
}
}
二 CharStreams
CharStreams
提供用於處理字符流的實用方法.
2.1 CharStreams常用方法
public final class CharStreams {
/**
* 數據複製
*/
@CanIgnoreReturnValue
public static long copy(Readable from, Appendable to) throws IOException;
/**
* Readable裏面的數據轉換爲String
*/
public static String toString(Readable r) throws IOException;
/**
* Readable裏面的數據按行讀出來放到List<String>裏面去。讀文件的時候經常用到
*/
@Beta
public static List<String> readLines(Readable r) throws IOException;
/**
* 一行一行的讀數據,一行一行的交給processor去處理
*/
@Beta
@CanIgnoreReturnValue // some processors won't return a useful result
public static <T> T readLines(Readable readable, LineProcessor<T> processor) throws IOException;
/**
* 從給定的Readable讀取並丟棄數據,直到到達流的末尾,相當於清空數據
*/
@Beta
@CanIgnoreReturnValue
public static long exhaust(Readable readable) throws IOException;
/**
* 從reader裏面丟棄指定的字節
*/
@Beta
public static void skipFully(Reader reader, long n) throws IOException;
/**
* 返回一個空的Writer,裏面什麼數據也沒得
*/
@Beta
public static Writer nullWriter();
/**
* 返回一個Writer,並且把target裏面的數據導入進去
*/
@Beta
public static Writer asWriter(Appendable target);
}
2.2 CharStreams簡單使用
// CharStreams.copy() 字符流拷貝
@Test
public void copy() {
URL url = Resources.getResource("application.yml");
File f = new File(url.getFile()); // 聲明File對象
try {
BufferedReader in = new BufferedReader(new FileReader(f));
StringBuilder stringBuilder = new StringBuilder();
CharStreams.copy(in, stringBuilder);
System.out.println(stringBuilder.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
// CharStreams.readLines() 一行,一行的讀取數據
@Test
public void readLines() {
URL url = Resources.getResource("application.yml");
File f = new File(url.getFile()); // 聲明File對象
try {
BufferedReader in = new BufferedReader(new FileReader(f));
List<String> lineList = CharStreams.readLines(in);
for (String lineItem : lineList) {
System.out.println(lineItem);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// CharStreams.readLines(),並且交給LineProcessor處理
@Test
public void readLines2() {
URL url = Resources.getResource("application.yml");
File f = new File(url.getFile()); // 聲明File對象
try {
BufferedReader in = new BufferedReader(new FileReader(f));
List<String> lineList = CharStreams.readLines(in, new LineProcessor<List<String>>() {
List<String> resultList = Lists.newArrayList();
@Override
public boolean processLine(String line) throws IOException {
resultList.add(line);
return true;
}
@Override
public List<String> getResult() {
return resultList;
}
});
// 打印結果
for (String lineItem : lineList) {
System.out.println(lineItem);
}
} catch (IOException e) {
e.printStackTrace();
}
}
三 Resources
提供用於處理類路徑中的資源的實用程序方法。
3.1 Resources常用方法
public final class Resources {
private Resources() {}
/**
* URL對應的數據,讀到ByteSource裏面
*/
public static ByteSource asByteSource(URL url);
/**
* URL對應的數據,讀到CharSource裏面
*/
public static CharSource asCharSource(URL url, Charset charset);
/**
* URL對應的數據讀到byte數組裏面去
*/
public static byte[] toByteArray(URL url) throws IOException;
/**
* URL對應數據讀到String裏面去
*/
public static String toString(URL url, Charset charset) throws IOException;
/**
* URL對應數據一行一行讀,一行一行給callback處理
*/
@CanIgnoreReturnValue // some processors won't return a useful result
public static <T> T readLines(URL url, Charset charset, LineProcessor<T> callback)
throws IOException;
/**
* URL數據一行一行讀出來放到List<String>裏面去
*/
public static List<String> readLines(URL url, Charset charset) throws IOException;
/**
* URL裏面數據拷貝到OutputStream裏面去
*/
public static void copy(URL from, OutputStream to) throws IOException;
/**
* 返回resourceName對應的java資源的URL
*/
@CanIgnoreReturnValue // being used to check if a resource exists
// TODO(cgdecker): maybe add a better way to check if a resource exists
// e.g. Optional<URL> tryGetResource or boolean resourceExists
public static URL getResource(String resourceName);
/**
* 同上,contextClass用來指定從contextClass所在路徑出發,去查找resourceName對應資源文件
*/
public static URL getResource(Class<?> contextClass, String resourceName);
}
3.2 Resources簡單使用
// Resources.getResource()
@Test
public void getResource() {
System.out.println(Resources.getResource("application.yml"));
// 起始路徑不一樣
System.out.println(Resources.getResource(ResourcesTest.class, "ResourcesTest.class"));
}
// Resources.readLines()
@Test
public void readLines() {
// 我們把application.yml文件的內容讀取出來
URL url = Resources.getResource("application.yml");
try {
// Resources.readLines
List<String> lineList = Resources.readLines(url, Charsets.UTF_8);
for (String lineItem : lineList) {
System.out.println(lineItem);
}
// Resources.readLines +
List<String> lineList2 = Resources.readLines(url, Charsets.UTF_8, new LineProcessor<List<String>>() {
List<String> lines = Lists.newArrayList();
@Override
public boolean processLine(String line) throws IOException {
lines.add(line);
return true;
}
@Override
public List<String> getResult() {
return lines;
}
});
for (String lineItem : lineList2) {
System.out.println(lineItem);
}
} catch (IOException e) {
e.printStackTrace();
}
}
四 Closeables
Closeables對象的實用方法。讓我們調用一些close方法更加的方便一點.
4.1 Closeables常用方法
public final class Closeables{
/**
* 調用可關閉對象Closeable的close方法.
*
* swallowIOException=true,不會拋出異常,false會拋出異常
*/
public static void close(@Nullable Closeable closeable, boolean swallowIOException)
throws IOException;
/**
* InputStream關閉
*/
public static void closeQuietly(@Nullable InputStream inputStream);
/**
* Reader關閉
*/
public static void closeQuietly(@Nullable Reader reader);
}
五 Flushables
Flushables對象的一些使用方法,讓我們調用flush方法更加的方便一點.
Flushables常用方法
public final class Flushables {
/**
* 把可以flush的Flushable對象,調用flush方法
* swallowIOException:true 不會拋出IOException異常,false會拋出IOException異常
*/
public static void flush(Flushable flushable, boolean swallowIOException) throws IOException;
/**
* flush(flushable, true);
*/
public static void flushQuietly(Flushable flushable);
}
六 Files
Files類提供使用文件相關的一些實用程序方法.
6.1 Files常用方法
public final class Files {
/**
* 把文件信息讀到BufferedReader裏面去
*/
@Beta
public static BufferedReader newReader(File file, Charset charset) throws FileNotFoundException;
/**
* 把文件信息和BufferedWriter關聯起來
*/
@Beta
public static BufferedWriter newWriter(File file, Charset charset) throws FileNotFoundException;
/**
* 把文件信息讀到ByteSource裏面去
*/
public static ByteSource asByteSource(File file);
/**
* 把文件裏面的內容以append追加方式(覆蓋方式可以省略)讀到ByteSink裏面去
*/
public static ByteSink asByteSink(File file, FileWriteMode... modes);
/**
* 把文件裏面的內容讀到CharSource裏面去
*/
public static CharSource asCharSource(File file, Charset charset);
/**
* 把文件裏面的內容讀到CharSink裏面去
*/
public static CharSink asCharSink(File file, Charset charset, FileWriteMode... modes);
/**
* 把文件裏面的內容讀到byte數組裏面去
*/
@Beta
public static byte[] toByteArray(File file) throws IOException;
/**
* 把文件裏面的內容讀到String裏面去
*/
@Beta
@Deprecated
public static String toString(File file, Charset charset) throws IOException;
/**
* byte數組裏面的內容寫到文件裏面去
*/
@Beta
public static void write(byte[] from, File to) throws IOException;
/**
* CharSequence內容寫到文件裏面去
*/
@Beta
@Deprecated
public static void write(CharSequence from, File to, Charset charset) throws IOException;
/**
* 把文件裏面的內容拷貝到OutputStream裏面去(個人認爲和讀出來的意思是一樣的)
*/
@Beta
public static void copy(File from, OutputStream to) throws IOException;
/**
* 把一個人家的內容拷貝到另一個文件裏面去
*/
@Beta
public static void copy(File from, File to) throws IOException;
/**
* 判斷兩個文件的內容是否相同
*/
@Beta
public static boolean equal(File file1, File file2) throws IOException;
/**
* 創建一個臨時文件(由java.io.tmpdir指定的操作系統緩存的臨時目錄下)
*/
@Beta
public static File createTempDir();
/**
* 創建一個空文件或更新上次更新的時間戳,與同名的unix命令相
*/
@Beta
@SuppressWarnings("GoodTime") // reading system time without TimeSource
public static void touch(File file) throws IOException;
/**
* 必要時爲文件創建父目錄(文件路徑裏面有父路徑)
*/
@Beta
public static void createParentDirs(File file) throws IOException;
/**
* 把文件從一個路徑移動到另一個路徑
*/
@Beta
public static void move(File from, File to) throws IOException;
/**
* 讀取文件的第一行數據
*/
@Beta
@Deprecated
public
static String readFirstLine(File file, Charset charset) throws IOException;
/**
* 一行,一行的把文件讀取出來
*/
@Beta
public static List<String> readLines(File file, Charset charset) throws IOException;
/**
* 一行,一行的把文件讀取出來,然後交給LineProcessor去處理
*/
@Beta
@Deprecated
@CanIgnoreReturnValue // some processors won't return a useful result
public
static <T> T readLines(File file, Charset charset, LineProcessor<T> callback) throws IOException;
/**
* 讀取文件並且把讀出來的內容交給ByteProcessor去處理
*/
@Beta
@Deprecated
@CanIgnoreReturnValue // some processors won't return a useful result
public
static <T> T readBytes(File file, ByteProcessor<T> processor) throws IOException;
/**
* 對文件做hash操作
*/
@Beta
@Deprecated
public
static HashCode hash(File file, HashFunction hashFunction) throws IOException;
/**
* 把文件的內容讀到MappedByteBuffer裏面去
* java nio中引入了一種基於MappedByteBuffer操作大文件的方式,其讀寫性能極高
*/
@Beta
public static MappedByteBuffer map(File file) throws IOException;
/**
* 把文件的內容讀到MappedByteBuffer裏面去
*/
@Beta
public static MappedByteBuffer map(File file, MapMode mode) throws IOException;
/**
* 把文件的內容讀到MappedByteBuffer裏面去
*/
@Beta
public static MappedByteBuffer map(File file, MapMode mode, long size) throws IOException;
/**
* 規範文件路徑,並不總是與文件系統一致,請仔細測試
*/
@Beta
public static String simplifyPath(String pathname);
/**
* 返回給定路徑所表示文件的擴展名
*/
@Beta
public static String getFileExtension(String fullName);
/**
* 返回去除了擴展名的文件名
*/
@Beta
public static String getNameWithoutExtension(String file);
/**
* 返回文件和目錄樹的raverser實例。返回的遍歷器從File開始,將返回它遇到的所有文件和目錄。
*/
@Beta
public static Traverser<File> fileTraverser() {
return Traverser.forTree(FILE_TREE);
}
/**
* 返回一個Predicate對象,用於判斷文件是否是目錄文件
*/
@Beta
public static Predicate<File> isDirectory();
/**
* 返回一個Predicate,用於判斷是否是文件
*/
@Beta
public static Predicate<File> isFile();
}
6.2 Files簡單使用
// Files.newReader() 把文件的內容讀到BufferedReader裏面去
@Test
public void newReader() {
// 這裏,需要換成你電腦存在的地址
File file = new File("/home/tuacy/github/google-guava-study/src/main/resources" + File.separator + "application.yml");
try {
BufferedReader bufferedReader = Files.newReader(file, Charsets.UTF_8);
List<String> lineList = CharStreams.readLines(bufferedReader);
for (String lineItem : lineList) {
System.out.println(lineItem);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// Files.newWriter
@Test
public void newWriter() {
File file = new File("/home/tuacy/github/google-guava-study/src/main/resources" + File.separator + "filewirite.txt");
try {
BufferedWriter bufferedWriter = Files.newWriter(file, Charsets.UTF_8);
bufferedWriter.write("hello word!!!");
// bufferedWriter.flush();
Flushables.flushQuietly(bufferedWriter);
} catch (IOException e) {
e.printStackTrace();
}
}
// Files.asByteSink
@Test
public void asByteSink() {
File file = new File("/home/tuacy/github/google-guava-study/src/main/resources" + File.separator + "filewirite.txt");
try {
ByteSink byteSink = Files.asByteSink(file, FileWriteMode.APPEND);
OutputStream outputStream = byteSink.openStream();
outputStream.write("hello word!!!".getBytes(Charsets.UTF_8));
// bufferedWriter.flush();
Flushables.flushQuietly(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
// 對文件做hash操作
@Test
public void hash() {
File file = new File("/home/tuacy/github/google-guava-study/src/main/resources" + File.separator + "filewirite.txt");
try {
HashCode hashCode = Files.asByteSource(file).hash(Hashing.sha256());
System.out.println(hashCode.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
// Files.fileTraverser() 用於遍歷文件
@Test
public void fileTraverser() {
Traverser<File> traverser = Files.fileTraverser();
File file = new File("/home/tuacy/github/google-guava-study/src/main/resources");
Iterable<File> list = traverser.breadthFirst(file);
list.forEach(new Consumer<File>() {
@Override
public void accept(File file) {
System.out.println(file.getName());
}
});
}
七 MoreFiles
MoreFiles類是Files類的一個補充類,MoreFiles裏面的方法也是操作文件相關的方法,不過MoreFiles針對的是Path類,關於Path類和File的的區別和用法可以自己去百度下,兩者都是操作文件的對象.
7.1 MoreFiles常用方法
public final class MoreFiles {
/**
* 文件內容讀到ByteSource裏面去
*/
public static ByteSource asByteSource(Path path, OpenOption... options);
/**
* 文件內容關聯ByteSink,這樣可以通過ByteSink把內容寫到文件裏面去
*/
public static ByteSink asByteSink(Path path, OpenOption... options);
/**
* 文件內容讀到CharSource裏面去u
*/
public static CharSource asCharSource(Path path, Charset charset, OpenOption... options);
/**
* 文件關聯CharSink,這樣可以通過CharSink把內容寫到文件裏面去
*/
public static CharSink asCharSink(Path path, Charset charset, OpenOption... options);
/**
* 獲取指定目錄下的文件
*/
public static ImmutableList<Path> listFiles(Path dir) throws IOException;
/**
* 返回Traverser,用於遍歷文件
*/
public static Traverser<Path> fileTraverser();
/**
* 返回一個表示目錄文件的Predicate,
*/
public static Predicate<Path> isDirectory(LinkOption... options);
/**
* 返回Predicate,用於判斷文件是不是一個正常的文件
*/
public static Predicate<Path> isRegularFile(LinkOption... options);
/**
* 兩個path對應的文件內容是否相同
*/
public static boolean equal(Path path1, Path path2) throws IOException;
/**
* 創建一個空文件或更新上次更新的時間戳
*/
@SuppressWarnings("GoodTime") // reading system time without TimeSource
public static void touch(Path path) throws IOException;
/**
* 創建父目錄
*/
public static void createParentDirectories(Path path, FileAttribute<?>... attrs)
throws IOException;
/**
* 返回給定路徑所表示文件的擴展名
*/
public static String getFileExtension(Path path);
/**
* 返回去除了擴展名的文件名
*/
public static String getNameWithoutExtension(Path path);
/**
* 刪除整個目錄
*/
public static void deleteRecursively(Path path, RecursiveDeleteOption... options)
throws IOException;
/**
* 刪除目錄下面的文件
*/
public static void deleteDirectoryContents(Path path, RecursiveDeleteOption... options)
throws IOException;
}
7.2 MoreFiles簡單使用
// MoreFiles.asCharSource()
@Test
public void asCharSource() {
Path path = Paths.get("/home/tuacy/github/google-guava-study/src/main/resources/abc.txt");
CharSource charSource = MoreFiles.asCharSource(path, Charsets.UTF_8);
try {
BufferedReader bufferedReader = charSource.openBufferedStream();
List<String> lines = CharStreams.readLines(bufferedReader);
for (String lineItem : lines) {
System.out.println(lineItem);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// MoreFiles.deleteDirectoryContents() 刪除目錄裏面的文件
// MoreFiles.deleteRecursively() 刪除目錄已經目錄裏面的文件
@Test
public void deleteDirectoryContents() {
Path path = Paths.get("/home/tuacy/github/google-guava-study/src/main/resources/abc");
try {
MoreFiles.deleteDirectoryContents(path, RecursiveDeleteOption.ALLOW_INSECURE);
MoreFiles.deleteDirectoryContents(path, RecursiveDeleteOption.ALLOW_INSECURE);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void createParentDirectories() {
Path path = Paths.get("/home/tuacy/github/google-guava-study/src/main/resources/abc/123/789/abc.txt");
try {
MoreFiles.createParentDirectories(path);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void isDirectory() {
Path path = Paths.get("/home/tuacy/github/google-guava-study/src/main/resources");
Predicate<Path> predicate = MoreFiles.isDirectory();
System.out.println("是否目錄 = " + predicate.apply(path));
}
關於Guava I/O 部分的內容,我們就先講這些主要是一些工具類的使用,熟悉ByteStreams,CharStreams,Resources,Closeables,Flushables,Files,MoreFiles這些個工具類裏面方法的使用. 充分的把他們用到我們實際開發當中去.相關實例代碼可以在 https://github.com/tuacy/google-guava-study 測試包下com.tuacy.guava.study.io包裏面找到.