Java Canonical Path Create getCanonicalPath(String path)

Here you can find the source of getCanonicalPath(String path)

Description

get Canonical Path

License

Apache License

Declaration

public static String getCanonicalPath(String path) 

Method Source Code


//package com.java2s;
/*//from ww w  .  j  a  va 2  s  .c  o m
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

import java.io.*;

public class Main {
    public static String getCanonicalPath(String path) {
        File f = openFile(path);
        return (f == null) ? null : getCanonicalPath(f);
    }

    public static String getCanonicalPath(File f) {
        try {
            return f.getCanonicalPath();
        } catch (IOException ex) {
            return f.getAbsolutePath();
        }
    }

    /**
     * Return an instance of File...
     *
     * @param path
     * @return File
     */
    public static File openFile(String path) {
        try {
            return new File(path);
        } catch (Error e) // J#.NET throws an error if the file is not found...
        {
            return null;
        }
    }

    public static File openFile(String path, boolean mkdir) {
        File f = new File(path).getAbsoluteFile();

        if (mkdir) {
            new File(f.getParent()).mkdirs();
        }

        return f;
    }

    /**
     * Return an instance of File...
     *
     * @param parentPath
     * @param fileName
     * @return File
     */
    public static File openFile(File parentPath, String fileName) {
        try {
            return new File(parentPath, fileName);
        } catch (Error e) // J#.NET throws an error if the file is not found...
        {
            return null;
        }
    }
}

Related

  1. getCanonicalPath(String inPath)
  2. getCanonicalPath(String name)
  3. getCanonicalPath(String outputPath)
  4. getCanonicalPath(String path)
  5. getCanonicalPath(String path)
  6. getCanonicalPath(String path)
  7. getCanonicalPath(String path)
  8. getCanonicalPath(String path)
  9. getCanonicalPath(String path)