Test if a given file is located inside the given directory. - Java File Path IO

Java examples for File Path IO:File Operation

Description

Test if a given file is located inside the given directory.

Demo Code


//package com.java2s;

import java.io.File;
import java.io.IOException;

public class Main {
    public static void main(String[] argv) throws Exception {
        File file = new File("Main.java");
        File dir = new File("Main.java");
        System.out.println(isLocatedInside(file, dir));
    }//from  w w  w.java2 s . co m

    /**
     * Test if a given file is located inside the given directory.
     * 
     * @param file
     * @param dir
     * @return
     * @throws IOException
     */
    public static boolean isLocatedInside(File file, File dir)
            throws IOException {
        return file.getCanonicalPath().startsWith(dir.getCanonicalPath());
    }
}

Related Tutorials