Graffiti Plum Graffiti Plum - 11 days ago 5x
Android Question

Working with Directories in Java/Android Application

I'd like to create a function to do the following: to replace an arbitrary string filled with potential "../" and "./" in the middle of it, to point to an absolute file name with the dots and extraneous slashes removed. ex: /data/data/org.hacktivity.datatemple/../../data/./org.hacktivity.datatemple/

private String validDirectory( String baseDirectory, String addOn ) {

if ( baseDirectory + addOn ISN'T A VALID DIRECTORY ) {
return baseDirectory;
else {
// ex: /data/data/org.hacktivity.datatemple/../org.hacktivity.datatemple/ => /data/data/org.hacktivity.datatemple
return TRIMMED VERSION OF baseDirectory + addOn;



You are searching for the canonicalPath of a File object. Use getCaconicalPath() or getCanonocalFile() to eliminate relative path elements:

File baseDir = new File(baseDirectory);
File addOnDir = new File(baseDir, addOn);

String canonicalPath = addOnDir.getCanonicalPath();

System.out.println(canonicalPath); // /data/data/org.hacktivity.datatemple