org.apache.commons.io.FilenameUtils.normalizeNoEndSeparator()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(10.7k)|赞(0)|评价(0)|浏览(200)

本文整理了Java中org.apache.commons.io.FilenameUtils.normalizeNoEndSeparator()方法的一些代码示例,展示了FilenameUtils.normalizeNoEndSeparator()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FilenameUtils.normalizeNoEndSeparator()方法的具体详情如下:
包路径:org.apache.commons.io.FilenameUtils
类名称:FilenameUtils
方法名:normalizeNoEndSeparator

FilenameUtils.normalizeNoEndSeparator介绍

[英]Normalizes a path, removing double and single dot path steps, and removing any final directory separator.

This method normalizes a path to a standard format. The input may contain separators in either Unix or Windows format. The output will contain separators in the format of the system.

A trailing slash will be removed. A double slash will be merged to a single slash (but UNC names are handled). A single dot path segment will be removed. A double dot will cause that path segment and the one before to be removed. If the double dot has no parent path segment to work with, null is returned.

The output will be the same on both Unix and Windows except for the separator character.

/foo//               -->   /foo 
/foo/./              -->   /foo 
/foo/../bar          -->   /bar 
/foo/../bar/         -->   /bar 
/foo/../bar/../baz   -->   /baz 
//foo//./bar         -->   /foo/bar 
/../                 -->   null 
../foo               -->   null 
foo/bar/..           -->   foo 
foo/../../bar        -->   null 
foo/../bar           -->   bar 
//server/foo/../bar  -->   //server/bar 
//server/../bar      -->   null 
C:\foo\..\bar        -->   C:\bar 
C:\..\bar            -->   null 
~/foo/../bar/        -->   ~/bar 
~/../bar             -->   null

(Note the file separator returned will be correct for Windows/Unix)
[中]规范化路径,删除双点和单点路径步骤,并删除任何最终目录分隔符。
此方法将路径规范化为标准格式。输入可以包含Unix或Windows格式的分隔符。输出将包含系统格式的分隔符。
后面的斜杠将被删除。双斜杠将合并为单斜杠(但会处理UNC名称)。将删除单个点路径段。双点将导致删除该路径段和之前的路径段。如果双点没有要使用的父路径段,则返回null
除了分隔符外,Unix和Windows上的输出将相同。

/foo//               -->   /foo 
/foo/./              -->   /foo 
/foo/../bar          -->   /bar 
/foo/../bar/         -->   /bar 
/foo/../bar/../baz   -->   /baz 
//foo//./bar         -->   /foo/bar 
/../                 -->   null 
../foo               -->   null 
foo/bar/..           -->   foo 
foo/../../bar        -->   null 
foo/../bar           -->   bar 
//server/foo/../bar  -->   //server/bar 
//server/../bar      -->   null 
C:\foo\..\bar        -->   C:\bar 
C:\..\bar            -->   null 
~/foo/../bar/        -->   ~/bar 
~/../bar             -->   null

(注意,返回的文件分隔符对于Windows/Unix是正确的)

代码示例

代码示例来源:origin: Alluxio/alluxio

/**
 * Checks and normalizes the given path.
 *
 * @param path The path to clean up
 * @return a normalized version of the path, with single separators between path components and
 *         dot components resolved
 * @throws InvalidPathException if the path is invalid
 */
public static String cleanPath(String path) throws InvalidPathException {
 validatePath(path);
 return FilenameUtils.separatorsToUnix(FilenameUtils.normalizeNoEndSeparator(path));
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testNormalizeNoEndSeparatorUnixWin() throws Exception {
  // Normalize (Unix Separator)
  assertEquals("/a/c", FilenameUtils.normalizeNoEndSeparator("/a/b/../c/", true));
  assertEquals("/a/c", FilenameUtils.normalizeNoEndSeparator("\\a\\b\\..\\c\\", true));
  // Normalize (Windows Separator)
  assertEquals("\\a\\c", FilenameUtils.normalizeNoEndSeparator("/a/b/../c/", false));
  assertEquals("\\a\\c", FilenameUtils.normalizeNoEndSeparator("\\a\\b\\..\\c\\", false));
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testNormalizeNoEndSeparator() throws Exception {
  assertEquals(null, FilenameUtils.normalizeNoEndSeparator(null));
  assertEquals(null, FilenameUtils.normalizeNoEndSeparator(":"));
  assertEquals(null, FilenameUtils.normalizeNoEndSeparator("1:\\a\\b\\c.txt"));
  assertEquals(null, FilenameUtils.normalizeNoEndSeparator("1:"));
  assertEquals(null, FilenameUtils.normalizeNoEndSeparator("1:a"));
  assertEquals(null, FilenameUtils.normalizeNoEndSeparator("\\\\\\a\\b\\c.txt"));
  assertEquals(null, FilenameUtils.normalizeNoEndSeparator("\\\\a"));
  assertEquals("a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalizeNoEndSeparator("a\\b/c.txt"));
  assertEquals("" + SEP + "a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalizeNoEndSeparator("\\a\\b/c.txt"));
  assertEquals("C:" + SEP + "a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalizeNoEndSeparator("C:\\a\\b/c.txt"));
  assertEquals("" + SEP + "" + SEP + "server" + SEP + "a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalizeNoEndSeparator("\\\\server\\a\\b/c.txt"));
  assertEquals("~" + SEP + "a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalizeNoEndSeparator("~\\a\\b/c.txt"));
  assertEquals("~user" + SEP + "a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalizeNoEndSeparator("~user\\a\\b/c.txt"));
  assertEquals("a" + SEP + "c", FilenameUtils.normalizeNoEndSeparator("a/b/../c"));
  assertEquals("c", FilenameUtils.normalizeNoEndSeparator("a/b/../../c"));
  assertEquals("c", FilenameUtils.normalizeNoEndSeparator("a/b/../../c/"));
  assertEquals(null, FilenameUtils.normalizeNoEndSeparator("a/b/../../../c"));
  assertEquals("a", FilenameUtils.normalizeNoEndSeparator("a/b/.."));
  assertEquals("a", FilenameUtils.normalizeNoEndSeparator("a/b/../"));
  assertEquals("", FilenameUtils.normalizeNoEndSeparator("a/b/../.."));
  assertEquals("", FilenameUtils.normalizeNoEndSeparator("a/b/../../"));
  assertEquals(null, FilenameUtils.normalizeNoEndSeparator("a/b/../../.."));
  assertEquals("a" + SEP + "d", FilenameUtils.normalizeNoEndSeparator("a/b/../c/../d"));
  assertEquals("a" + SEP + "d", FilenameUtils.normalizeNoEndSeparator("a/b/../c/../d/"));
  assertEquals("a" + SEP + "b" + SEP + "d", FilenameUtils.normalizeNoEndSeparator("a/b//d"));
  assertEquals("a" + SEP + "b", FilenameUtils.normalizeNoEndSeparator("a/b/././."));

代码示例来源:origin: naver/ngrinder

/**
 * Join two path without appending '/'.
 *
 * @param path1 path1
 * @param path2 path2
 * @return joined path
 */
public static String join(String path1, String path2) {
  path1 = trimPathSeparatorBothSides(path1);
  path2 = trimPathSeparatorBothSides(path2);
  return FilenameUtils.normalizeNoEndSeparator(path1 + "/" + path2, true);
}

代码示例来源:origin: Teradata/kylo

final String name = FilenameUtils.getName(FilenameUtils.normalizeNoEndSeparator(url.getPath()));

代码示例来源:origin: geotools/geotools

String normalizedTargetPath = FilenameUtils.normalizeNoEndSeparator(targetPath);
String normalizedBasePath = FilenameUtils.normalizeNoEndSeparator(basePath);

代码示例来源:origin: jshiell/checkstyle-idea

@NotNull final String pathSeparator) {
String normalizedTargetPath = FilenameUtils.normalizeNoEndSeparator(targetPath);
String normalizedBasePath = FilenameUtils.normalizeNoEndSeparator(basePath);

代码示例来源:origin: zhoulychn/mybatis-generator

/**
 * 规范化路径,合并其中的多个分隔符为一个,并转化为本地系统路径格式,若是路径,则不带最后的路径分隔符
 *
 * @param filename
 *            文件完整路径
 * @return
 */
public static String normalizeNoEndSeparator(String filename) {
  return FilenameUtils.normalizeNoEndSeparator(filename);
}

代码示例来源:origin: nordicway/moji

public String normalizeFilename(String filename) {
  String result = Normalizer.normalize(filename, Normalizer.Form.NFD);
  result = FilenameUtils.normalizeNoEndSeparator(result, true)
      .replaceAll("[^\\p{ASCII}]", "");
  return result;
}

代码示例来源:origin: com.github.mike10004/crxtool-core

private String normalize(Path path) {
  Path relativeDir = enclosure.relativize(path);
  return FilenameUtils.normalizeNoEndSeparator(relativeDir.toString(), true);
}

代码示例来源:origin: org.apache.jackrabbit/oak-blob-plugins

@Override public boolean apply(File input) {
    return input.isFile() && !normalizeNoEndSeparator(input.getParent())
      .equals(cacheRoot.getAbsolutePath());
  }
}).iterator();

代码示例来源:origin: apache/jackrabbit-oak

@Override public boolean apply(File input) {
    return input.isFile() && !normalizeNoEndSeparator(input.getParent())
      .equals(cacheRoot.getAbsolutePath());
  }
}).iterator();

代码示例来源:origin: org.apache.sling/org.apache.sling.testing.sling-mock-oak

@Override public boolean apply(File input) {
    return input.isFile() && !normalizeNoEndSeparator(input.getParent())
      .equals(cacheRoot.getAbsolutePath());
  }
}).iterator();

代码示例来源:origin: intuit/Tank

/**
 * @param basePath
 */
public FileSystemFileStorage(String basePath, boolean compress) {
  super();
  this.basePath = FilenameUtils.normalizeNoEndSeparator(basePath);
  this.compress = compress;
  File dir = new File(basePath);
  if (!dir.exists()) {
    LOG.info("Creating storage dir " + dir.getAbsolutePath());
  }
}

代码示例来源:origin: kasonyang/kalang

public static String getClassName(File dir,File file){
  String dirPath = FilenameUtils.normalizeNoEndSeparator(dir.getAbsolutePath());
  String fname =FilenameUtils.normalizeNoEndSeparator(file.getAbsolutePath());
  String ext = FilenameUtils.getExtension(fname);
  String clsName = fname.substring(dirPath.length() + 1, fname.length() - ext.length()-1).replace(File.separator, ".");
  return clsName;
}

代码示例来源:origin: huangjian888/jeeweb-mybatis-springboot

private static final File getAbsoluteFile(String uploadDir, String filename) throws IOException {
  uploadDir = FilenameUtils.normalizeNoEndSeparator(uploadDir);
  System.out.println("getAbsoluteFile path:"+uploadDir + "/" + filename);
  File desc = new File(uploadDir + "/" + filename);
  if (!desc.getParentFile().exists()) {
    desc.getParentFile().mkdirs();
  }
  if (!desc.exists()) {
    desc.createNewFile();
  }
  return desc;
}

代码示例来源:origin: net.sf.patterntesting/patterntesting-rt

/**
 * Instantiates a new class walker.
 *
 * @param dir the dir
 */
public ClassWalker(File dir) {
  super(getFileFilter(), -1);
  this.startDir = dir;
  this.startDirnameLength = FilenameUtils
      .normalizeNoEndSeparator(dir.getAbsolutePath()).length();
}

代码示例来源:origin: raydac/netbeans-mmd-plugin

public FileVertex(@Nonnull final File file, @Nonnull final FileVertexType type) {
 this.type = type;
 this.text = file.getName();
 this.tooltip = "<html><b>" + type.toString() + "</b><br>" + StringEscapeUtils.unescapeHtml(FilenameUtils.normalizeNoEndSeparator(file.getAbsolutePath())) + "</html>"; //NOI18N
 this.file = file;
}

代码示例来源:origin: reficio/soap-ws

private static String constructResourcePath(String packagePath, String resourceName) {
  String resourcePath = String.format("/%s/%s", packagePath, resourceName);
  String resourcePathUnixSeparators = FilenameUtils.separatorsToUnix(resourcePath);
  String resourcePathNoLeadingSeparators = removeLeadingUnixSeparators(resourcePathUnixSeparators);
  String normalizedResourcePath = FilenameUtils.normalizeNoEndSeparator(resourcePathNoLeadingSeparators, true);
  return normalizedResourcePath;
}

代码示例来源:origin: org.alluxio/alluxio-core-common

/**
 * Checks and normalizes the given path.
 *
 * @param path The path to clean up
 * @return a normalized version of the path, with single separators between path components and
 *         dot components resolved
 * @throws InvalidPathException if the path is invalid
 */
public static String cleanPath(String path) throws InvalidPathException {
 validatePath(path);
 return FilenameUtils.separatorsToUnix(FilenameUtils.normalizeNoEndSeparator(path));
}

相关文章