|
By Prasanna Sherekar |
Check if File Exists in Internal and External Storage – Android and Java Example
— Check if File Exists in Android Internal Storage
|
public boolean fileExists(Context context, String filename) { File file = context.getFileStreamPath(filename); if (file != null && file.exists()) { return true; } return false; } |
— Check if File Exists in Android External Storage
|
public boolean fileExists() { String fileName = "/appname/data.xml"; String filePath = Environment.getExternalStorageDirectory().getPath() + fileName; File file = new File(filePath); return file.exists(); } |
|
By Prasanna Sherekar |
|
import java.io.File; import org.apache.commons.io.FileUtils; public class DirectorySizeSample { public static void main(String[] args) { // Using FileUtils.sizeOfDirectory() we can calculate // the size of a directory including it sub directory long size = FileUtils.sizeOfDirectory(new File("C:/Windows")); System.out.println("Size: " + size + " bytes"); } } |
|
By Prasanna Sherekar |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
import org.apache.commons.io.FileUtils; import java.io.File; import java.net.URL; public class URLToFile { public static void main(String[] args) throws Exception { // // FileUtils.toFile(URL url) convert from URL the File. // String data = FileUtils.readFileToString(FileUtils.toFile( URLToFile.class.getResource("/data.txt"))); System.out.println("data = " + data); // // Creates a URL with file protocol and convert it into File // object. // File file = FileUtils.toFile(new URL("file://D:/demo.txt")); data = FileUtils.readFileToString(file); System.out.println("data = " + data); } } |