How to read|write|copy|delete on android using guava library
guava-libraries :
Code Example write file on android
Write file : /storage/sdcard/Android/data/package_name/files/hackedbyak1.txt
Write file : /storage/sdcard/Android/data/package_name/files/hackedbyak1.txt
Note : การใช้ Files.method(); จะต้องอยู่ใน try catch : IOException เท่านั้น !
Write File
final File file = new File(fileName);
Files.write(content.getBytes(), file);
Copy File
final File sourceFile = new File(sourceFileName);
final File targetFile = new File(targetFileName);
Files.copy(sourceFile, targetFile);
Move File
final File sourceFile = new File(sourceFileName);
final File targetFile = new File(targetFileName);
Files.move(sourceFile, targetFile);
Comparing Files
final File file1 = new File(fileName1);
final File file2 = new File(fileName2);
Log.e( "File : "+fileName1+"+ (Files.equal(file1, file2) ? "IS" : "is NOT")+"the same as file '"+ fileName2);
Retrieving File Contents
final File sourceFile = new File("filename");
final String fileContents = Files.toString(sourceFile, Charset.defaultCharset());
Log.e("Contents of File '" + filename + " are: " + fileContents);
Retrieving Contents of File as Lines
final int i = 0;
final List<String> lines = Files.readLines(file, Charset.defaultCharset());
for (final String line : lines) {
Log.e("line : "+i+" ' "+line);
i++;
}
Reading File's First Line
final File file = new File(fileName);
final String line = Files.readFirstLine(file, Charset.defaultCharset());
--------------------------------------------------------
Credit : http://www.javaworld.com/article/2074453/core-java/file-management-in-java-with-guava-s-files-class.html