Java File System toSystemPath(String rawpath)

Here you can find the source of toSystemPath(String rawpath)

Description

As the declared paths in this testcase might be actual paths on the system running these tests, the expected paths should be cleaned up to represent the actual system paths.

License

Open Source License

Declaration

public static String toSystemPath(String rawpath) 

Method Source Code


//package com.java2s;
//  are made available under the terms of the Eclipse Public License v1.0

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    /**//  ww w  .  j a v  a 2s.  c  o  m
     * As the declared paths in this testcase might be actual paths on the system
     * running these tests, the expected paths should be cleaned up to represent
     * the actual system paths.
     * <p>
     * Eg: on fedora /etc/init.d is a symlink to /etc/rc.d/init.d
     */
    public static String toSystemPath(String rawpath) {
        Path path = FileSystems.getDefault().getPath(rawpath);
        if (Files.exists(path)) {
            // It exists, resolve it to the real path
            try {
                path = path.toRealPath();
            } catch (IOException e) {
                // something prevented us from resolving to real path, fallback to
                // absolute path resolution (not as accurate)
                path = path.toAbsolutePath();
                e.printStackTrace();
            }
        } else {
            // File doesn't exist, resolve to absolute path
            // We can't rely on File.toCanonicalPath() here
            path = path.toAbsolutePath();
        }
        return path.toString();
    }
}

Related

  1. isFileSystemAvailable2(final Path file, final String topLevelAbsolutePath)
  2. loadSystemResourceKeyValueCsvFileToMap(String resourcePath)
  3. newJarFileSystem(Path jarFilePath)
  4. newOutputStream(FileSystem system, Path path)
  5. systemEnvironmentPaths()
  6. zipDirectoryOrFile(String level, Path target, FileSystem zipFileFileSystem)