Java Path File Name nio getAbsolutePath(String fileName)

Here you can find the source of getAbsolutePath(String fileName)

Description

Returns the absolute path to a file with name fileName

License

Apache License

Parameter

Parameter Description
fileName the name of a file

Return

absolute path to the file

Declaration

public static String getAbsolutePath(String fileName) 

Method Source Code


//package com.java2s;
/*/*from  ww  w  .ja v  a 2 s  .  co m*/
 * Copyright 2016 Red Hat, Inc.
 *
 * Red Hat 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.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;

public class Main {
    /**
     * Returns the absolute path to a file with name <code>fileName</code>
     * @param fileName the name of a file
     * @return absolute path to the file
     */
    public static String getAbsolutePath(String fileName) {
        return Paths.get(fileName).toAbsolutePath().toString();
    }

    /**
     * Returns the absolute path to a resource addressed by the given <code>url</code>
     * @param resource URL
     * @return absolute path to the resource
     */
    public static String getAbsolutePath(URL url) {
        try {
            return url != null ? Paths.get(url.toURI()).toAbsolutePath().toString() : null;
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. findFile(String basePath, final String fileName)
  2. findNextFileName(Path folder, String name)
  3. findRootPathForResource(String resourceName, ClassLoader classLoader)
  4. findUpward(String filename, Path startingPath)
  5. formatPathName(Path path)
  6. getAbsolutePath(String name)
  7. getAbsolutePathName(File dir)
  8. getAllFiles(List fileNames, Path path)
  9. getArtifactName(Path artifactPath)