Java Canonical Path Create getCanonicalDirectory(final String rawDir)

Here you can find the source of getCanonicalDirectory(final String rawDir)

Description

If the given string is the empty string, then the result is the current directory.

License

Apache License

Parameter

Parameter Description
rawDir a path in any legal form, such as a relative path

Return

the absolute and unique path in String

Declaration

public static String getCanonicalDirectory(final String rawDir) throws IOException 

Method Source Code


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

import java.io.File;

import java.io.IOException;

public class Main {
    /**//  ww w.  j  av  a 2  s .co  m
     * If the given string is the empty string, then the result is the current directory.
     *
     * @param rawDir a path in any legal form, such as a relative path
     * @return the absolute and unique path in String
     */
    public static String getCanonicalDirectory(final String rawDir) throws IOException {
        String dir = rawDir.length() == 0 ? "." : rawDir;

        // create working dir if necessary
        File dd = new File(dir);
        if (!dd.exists()) {
            dd.mkdirs();
        }

        if (!dir.startsWith("/")) {
            dir = dd.getCanonicalPath();
        }

        return dir;
    }
}

Related

  1. getCanonicalCharset(String charset)
  2. getCanonicalCharsetQuiet(String charset)
  3. getCanonicalDirectory(String path, String errPrefix)
  4. getCanonicalFile(@Nullable final File aFile)
  5. getCanonicalFile(File f)
  6. getCanonicalFile(File file)