Example usage for java.io File equals

List of usage examples for java.io File equals

Introduction

In this page you can find the example usage for java.io File equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Tests this abstract pathname for equality with the given object.

Usage

From source file:Main.java

static boolean copyFiles(File sourceLocation, File targetLocation) throws IOException {
    if (sourceLocation.equals(targetLocation))
        return false;

    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            targetLocation.mkdir();//from  w ww  .ja v  a2  s.com
        }
        String[] children = sourceLocation.list();
        for (int i = 0; i < children.length; i++) {
            copyFiles(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
        }
    } else if (sourceLocation.exists()) {
        InputStream in = new FileInputStream(sourceLocation);
        OutputStream out = new FileOutputStream(targetLocation);

        // Copy the bits from instream to outstream
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
    return true;
}

From source file:Main.java

public static boolean isPathUnderAppName(String appName, File path) {

    File parentDir = new File(getAppFolder(appName));

    while (path != null && !path.equals(parentDir)) {
        path = path.getParentFile();/*from  w  w w.jav a  2 s .c om*/
    }

    return (path != null);
}

From source file:Main.java

public static void copyFile(File in, File out) throws IOException {
    // avoids copying a file to itself
    if (in.equals(out)) {
        return;//from   w  w w  .ja  va2  s  .c  om
    }
    // ensure the output file location exists
    out.getCanonicalFile().getParentFile().mkdirs();

    BufferedInputStream fis = new BufferedInputStream(new FileInputStream(in));
    BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(out));

    // a temporary buffer to read into
    byte[] tmpBuffer = new byte[8192];
    int len = 0;
    while ((len = fis.read(tmpBuffer)) != -1) {
        // add the temp data to the output
        fos.write(tmpBuffer, 0, len);
    }
    // close the input stream
    fis.close();
    // close the output stream
    fos.flush();
    fos.close();
}

From source file:Main.java

/**
 * Get a list of external SD card paths. (Kitkat or higher.)
 *
 * @return A list of external SD card paths.
 *//*from  ww w .ja  va 2 s  . c om*/
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String[] getExtSdCardPaths() {
    List<String> paths = new ArrayList<>();
    for (File file : applicationContext.getExternalFilesDirs("external")) {
        if (file != null && !file.equals(applicationContext.getExternalFilesDir("external"))) {
            int index = file.getAbsolutePath().lastIndexOf("/Android/data");
            if (index < 0) {
                Log.w("Uri", "Unexpected external file dir: " + file.getAbsolutePath());
            } else {
                String path = file.getAbsolutePath().substring(0, index);
                try {
                    path = new File(path).getCanonicalPath();
                } catch (IOException e) {
                    // Keep non-canonical path.
                }
                paths.add(path);
            }
        }
    }
    return paths.toArray(new String[paths.size()]);
}

From source file:Main.java

/**
 * Returns the relative path beginning after the getAppFolder(appName) directory.
 * The relative path does not start or end with a '/'
 *
 * @param appName// w  w w  .j a  v a  2 s  .co  m
 * @param fileUnderAppName
 * @return
 */
public static String asRelativePath(String appName, File fileUnderAppName) {
    // convert fileUnderAppName to a relative path such that if
    // we just append it to the AppFolder, we have a full path.
    File parentDir = new File(getAppFolder(appName));

    ArrayList<String> pathElements = new ArrayList<String>();

    File f = fileUnderAppName;
    while (f != null && !f.equals(parentDir)) {
        pathElements.add(f.getName());
        f = f.getParentFile();
    }

    if (f == null) {
        throw new IllegalArgumentException("file is not located under this appName (" + appName + ")!");
    }

    StringBuilder b = new StringBuilder();
    for (int i = pathElements.size() - 1; i >= 0; --i) {
        String element = pathElements.get(i);
        b.append(element);
        if (i != 0) {
            b.append(File.separator);
        }
    }
    return b.toString();

}

From source file:com.playonlinux.core.utils.Files.java

public static boolean isInSubDirectory(File directory, File fileIside) {
    return fileIside != null
            && (fileIside.equals(directory) || isInSubDirectory(directory, fileIside.getParentFile()));
}

From source file:Main.java

public static String extractAppNameFromPath(File path) {

    if (path == null) {
        return null;
    }//from w  ww. j  av  a2s . c om

    File parent = path.getParentFile();
    File odkDir = new File(getOdkFolder());
    while (parent != null && !parent.equals(odkDir)) {
        path = parent;
        parent = path.getParentFile();
    }

    if (parent == null) {
        return null;
    } else {
        return path.getName();
    }
}

From source file:au.org.ala.delta.util.FileUtils.java

private static File parent(File start, File parent) {
    if (start.equals(parent) || start.getParentFile() == null) {
        return start;
    } else {//from  w  w  w .j  ava2 s . c  o m
        return parent(start.getParentFile(), parent);
    }
}

From source file:com.manydesigns.elements.util.ElementsFileUtils.java

public static String getRelativePath(File ancestor, File file, String separator) {
    String path = file.getName();
    File parent = file.getParentFile();
    while (parent != null && !parent.equals(ancestor)) {
        path = parent.getName() + separator + path;
        parent = parent.getParentFile();
    }//from w w w .  j ava  2s .  c om
    return path;
}

From source file:Main.java

public static boolean equals(File file1, File file2) {
    try {/*  w ww. jav a 2  s . co m*/
        file1 = file1.getCanonicalFile();
        file2 = file2.getCanonicalFile();
    } catch (IOException ignore) {
        return false;
    }
    return file1.equals(file2);
}