Files class
provides number of utility methods to work with Files. By using Files class you
can do following functions effectively.
Copying files
Files class
provides copy method to copy contents of a file to another file.
Method
|
Description
|
public static
void copy(File from, OutputStream to) throws IOException
|
Copy contents
of a file to output stream.
|
public static
void copy(File src, File destination) throws IOException
|
Copy contents
of src file to destination file. If destination file already exists, then
contents of destination file are overwritten.
|
public static
void copy(File from, Charset charset, Appendable to)
|
Copies
contents of a file to an appendable object,using the given character set.
|
Copy source file to destination
import java.io.File; import java.io.IOException; import com.google.common.io.Files; public class FilesEx { public static void main(String args[]) throws IOException { String src = "/Users/harikrishna_gurram/emp.txt"; String destination = "/Users/harikrishna_gurram/emp_new.txt"; System.out.println("Copying file from " + src + " to destination " + destination); Files.copy(new File(src), new File(destination)); System.out.println("File copying done"); } }
Copy file data to console
import java.io.File; import java.io.IOException; import com.google.common.io.Files; public class FilesEx { public static void main(String args[]) throws IOException { String src = "/Users/harikrishna_gurram/emp.txt"; System.out.println(src + " contents are"); Files.copy(new File(src), System.out); } }
Move file from one location to another
Files class provides move method to
move file from one location to another.
public static void move(File from, File to) throws IOException
import java.io.File; import java.io.IOException; import com.google.common.io.Files; public class FilesEx { public static void main(String args[]) throws IOException { String src = "/Users/harikrishna_gurram/emp.txt"; String destination = "/Users/harikrishna_gurram/emp_new.txt"; Files.move(new File(src), new File(destination)); } }
Read file data as list of strings
Files class provides following methods
to read file data as list of string. Each element in list represents a line of
file.
Method
|
Description
|
public static String
readFirstLine(File file, Charset charset)
|
Reads the first line from a file,
charset used to decode the input stream.
|
public static List<String>
readLines(File file, Charset charset)
|
Read all the lines from a file.
|
public static <T> T
readLines(File file, Charset charset,LineProcessor<T> callback)
|
Streams lines from a File, stopping
when our callback returns false, or we have read all of the lines. Returns
the output of processing the lines
|
Read first line of file
import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import com.google.common.io.Files; public class FilesEx { public static void main(String args[]) throws IOException { String src = "/Users/harikrishna_gurram/emp.txt"; String firstLine = Files.readFirstLine(new File(src), Charset.defaultCharset()); System.out.println(firstLine); } }
Read all the lines from a file
import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.util.List; import com.google.common.io.Files; public class FilesEx { public static void main(String args[]) throws IOException { String src = "/Users/harikrishna_gurram/emp.txt"; List<String> data = Files.readLines(new File(src), Charset.defaultCharset()); for (String line : data) { System.out.println(line); } } }
Read specific content from file as list of strings
Let’s say I had following csv file,
which contains author name, title and price. I want to extract all the titles from
csv file.
Harper Lee, To Kill a Mockingbird, 250
J.K. Rowling, Harry Potter and the
Deathly Hallows, 450
Suzanne Collins, The Hunger Games, 325
Orson Scott Card, Ender's Game, 876
Jane Austen, Pride and Prejudice, 234
Markus Zusak, The Book Thief, 345
J.K. Rowling, Harry Potter and the
Sorcerer's Stone, 546
First column contains author name,
second column contains book title and third column contains price.
import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.util.List; import com.google.common.base.Splitter; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.io.Files; import com.google.common.io.LineProcessor; public class FilesEx { public static class TitlesExtractor implements LineProcessor<List<String>> { Splitter splitter = Splitter.on(","); List<String> authors = Lists.newArrayList(); @Override public boolean processLine(String line) throws IOException { Iterable<String> columns = splitter.split(line); String author = Iterables.get(columns, 1); authors.add(author); return true; } @Override public List<String> getResult() { return authors; } } public static void main(String args[]) throws IOException { String src = "/Users/harikrishna_gurram/books.csv"; List<String> titles = Files.readLines(new File(src), Charset.defaultCharset(), new TitlesExtractor()); for (String line : titles) { System.out.println(line); } } }
Output
To Kill a Mockingbird Harry Potter and the Deathly Hallows The Hunger Games Ender's Game Pride and Prejudice The Book Thief Harry Potter and the Sorcerer's Stone
Get Hash value for a file
You can compute the hash value for a
file using hash function.
public static HashCode hash(File file, HashFunction hashFunction)
import java.io.File; import java.io.IOException; import com.google.common.hash.HashCode; import com.google.common.hash.Hashing; import com.google.common.io.Files; public class FilesEx { public static void main(String args[]) throws IOException { String src = "/Users/harikrishna_gurram/books.csv"; HashCode hashCode = Files.hash(new File(src), Hashing.sha256()); System.out.println("Hash code for given file is "); System.out.println(hashCode); } }
Writing data to files
Files class provides write method to
write data to file.
public static void write(CharSequence from, File to, Charset charset)
Writes a character sequence to a file
using the given character set.
import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import com.google.common.io.Files; public class FilesEx { public static void main(String args[]) throws IOException { String to = "/Users/harikrishna_gurram/sample.txt"; File file = new File(to); String data = "Hello, Welcome to Guava package\nHope you are enjoying tutorial"; Files.write(data, file, Charset.defaultCharset()); } }
Append data to file
Files class provides append method to
append data to a file.
public static void append(CharSequence from, File to, Charset charset)
Appends a character sequence to a file
using the given character set.
import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import com.google.common.io.Files; public class FilesEx { public static void main(String args[]) throws IOException { String to = "/Users/harikrishna_gurram/sample.txt"; File file = new File(to); for (int i = 0; i < 20; i++) { Files.append("line " + i + "\n", file, Charset.defaultCharset()); } } }
No comments:
Post a Comment