It is believed that many people began to use the JDK Library to operate the file, the tedious duplication of code is disgusting, of course, this is the reason for the various libraries. The next introduction to the files class of guava provides a great simplification of file operations, such as the ability of a programmer to switch off a stream without worrying about it at all.
This paper describes the basic operation of files such as copying, renaming, and content reading.
code example:
Import Com.google.common.base.charsets;import Com.google.common.collect.lists;import Com.google.common.hash.hashcode;import Com.google.common.hash.hashing;import Com.google.common.io.*;import Java.io.*;import java.nio.charset.charset;import java.util.list;/** * Created by Wenniuwuren on 2015/6/16. */public class Filestest {public static void main (string[] args) {try {filestest filestest = new F Ilestest (); One file content is copied to another file/*file original = new ("E:\\test\\original"); File copy = new file ("E:\\test\\copy"); if (Original.isfile () && copy.isfile ()) {files.copy (original, copy); }*///File renaming */* File Originalfile = new file ("E:\\test\\1.txt"); File NewFile = new file ("E:\\test\\2.txt"); if (Originalfile.isfile () && newfile.isfile ()) {Files.move (originalfile, newFile); }*///reads the contents of the file one line at a line File File = new file ("E:\\test\\1.txt"); list<string> expectedlines = lists.newarraylist ("The Quick Brown", "Fox jumps Over", "the Lazy Dog"); list<string> readlines = files.readlines (file, charsets.utf_8); System.out.printf (Readlines.tostring ()); Generates a hashcode hashcode hashcode = files.hash (file, Hashing.md5 ()) for the file; System.out.println (hashcode); File Write/Add nothing to care about open open stream/close stream String Hamletquotestart = "To is, or not to is"; Files.write (Hamletquotestart, file, charsets.utf_8); String hamletquoteend = "That's a question"; Files.append (hamletquoteend, file, charsets.utf_8); Sources read Sinks write//Sources and Sinks not streams ', readers ', or Writers ' object//But provide the same functionality File Dest = new file ("E:\\test\\2.txt"); Dest.deleteonexit (); File Source = new file ("E:\\test\\1.txt"); ByteSOurce Bytesource = files.asbytesource (source); Bytesink Bytesink = Files.asbytesink (dest); Bytesource.copyto (Bytesink); System.out.println (Bytesink + "," + Bytesource); Write the contents of several files to the same file F1 = new file ("E:\\test\\1.txt"); File F2 = new file ("E:\\test\\2.txt"); File F3 = new file ("E:\\test\\3.txt"); File Joinedoutput = new file ("E:\\test\\4.txt"); Joinedoutput.deleteonexit (); Generate Inputsupplier for each file (use closer to manage shutdown of the underlying I/O resources) list<inputsupplier<inputstreamreader>> Inputsuppliers = Filestest.getinputsuppliers (F1, F2, F3); Logically merged into a inputsupplier inputsupplier<reader> joinedsupplier = Charstreams.join (inputsuppliers); Build output outputsupplier<outputstreamwriter> Outputsupplier = Files.newwritersupplier (JoinedOutput, Ch Arsets. UTF_8); Write the underlying inputsuppliers to Outputsupplier CharstreamS.copy (Joinedsupplier, outputsupplier); } catch (IOException e) {e.printstacktrace (); }} private String Joinfiles (File ... files) throws IOException {StringBuilder builder = new StringBuilder (); for (file file:files) {builder.append (files.tostring (file, charsets.utf_8)); } return builder.tostring (); } private list<inputsupplier<inputstreamreader>> getinputsuppliers (File ... files) {List<inputsup plier<inputstreamreader>> list = Lists.newarraylist (); for (file file:files) {list.add (files.newreadersupplier (file, charsets.utf_8)); } return list; }}
Resources:
"Getting Started with Google guava"
Guava----Files file operations for the development of efficient exploded tables