Gets the individual path elements building up the canonical path to the given file. - Java File Path IO

Java examples for File Path IO:Path

Description

Gets the individual path elements building up the canonical path to the given file.

Demo Code

// Copyright (C) 2001-2012 Michael Bayne, et al.
//package com.java2s;
import java.io.File;
import java.io.IOException;

public class Main {
    /**//from  w w w  .  j  a  v  a  2  s  .c om
     * Gets the individual path elements building up the canonical path to the given file.
     */
    public static String[] getCanonicalPathElements(File file)
            throws IOException {
        file = file.getCanonicalFile();

        // If we were a file, get its parent
        if (!file.isDirectory()) {
            file = file.getParentFile();
        }

        return file.getPath().split(File.separator);
    }
}

Related Tutorials