Java Path File Name nio shortenFileName(Path file, List dirs)

Here you can find the source of shortenFileName(Path file, List dirs)

Description

Converts an absolute file to a relative one, if possible.

License

Open Source License

Parameter

Parameter Description
file the file to be shortened
dirs directories to check

Declaration

public static Path shortenFileName(Path file, List<Path> dirs) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.nio.file.Path;

import java.util.List;

public class Main {
    /**/*from  www.  ja va2  s . c  om*/
     * Converts an absolute file to a relative one, if possible.
     * Returns the parameter file itself if no shortening is possible
     * <p>
     * This method works correctly only if dirs are sorted decent in their length
     * i.e. /home/user/literature/important before /home/user/literature
     *
     * @param file the file to be shortened
     * @param dirs directories to check
     */
    public static Path shortenFileName(Path file, List<Path> dirs) {
        if (!file.isAbsolute()) {
            return file;
        }

        for (Path dir : dirs) {
            if (file.startsWith(dir)) {
                return dir.relativize(file);
            }
        }
        return file;
    }
}

Related

  1. renameFile(Path filePath, String postfix)
  2. renameLogFile(Path original)
  3. saveFileFromByteArray(String filename, String pathDirectory, byte[] data)
  4. saveNamesToFile(Map namesMap, Path namesFile)
  5. saveToFile(String content, String directoryPath, String filename)
  6. storeFile(String fileName, InputStream inputStream, Path targetPath)
  7. subDirectoryNames(final Path dir)
  8. toClassName(String path)
  9. toResourcePath(String fileName)