Here you can find the source of shortenFileName(Path file, List
Parameter | Description |
---|---|
file | the file to be shortened |
dirs | directories to check |
public static Path shortenFileName(Path file, List<Path> dirs)
//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; } }