Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.IOException;

public class Main {
    public static boolean isAncestor(File ancestorCandidate, File childCandidtate) {
        File parent = childCandidtate;
        while (true) {
            parent = parent.getParentFile();
            if (parent == null) {
                return false;
            }
            if (isSame(parent, ancestorCandidate)) {
                return true;
            }
        }
    }

    public static boolean isSame(File file1, File file2) {
        return canonicalFile(file1).equals(canonicalFile(file2));
    }

    public static boolean equals(File fileA, File fileB) {
        return canonicalFile(fileA).equals(canonicalFile(fileB));
    }

    /**
     * A 'checked exception free' version of {@link File#getCanonicalFile()}.
     */
    public static File canonicalFile(File file) {
        try {
            return file.getCanonicalFile();
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
    }
}