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

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

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

FilenameUtils.getPrefix介绍

[英]Gets the prefix from a full filename, such as C:/ or ~/.

This method will handle a file in either Unix or Windows format. The prefix includes the first slash in the full filename where applicable.

Windows: 
a\b\c.txt           --> ""          --> relative 
\a\b\c.txt          --> "\"         --> current drive absolute 
C:a\b\c.txt         --> "C:"        --> drive relative 
C:\a\b\c.txt        --> "C:\"       --> absolute 
\\server\a\b\c.txt  --> "\\server\" --> UNC 
Unix: 
a/b/c.txt           --> ""          --> relative 
/a/b/c.txt          --> "/"         --> absolute 
~/a/b/c.txt         --> "~/"        --> current user 
~                   --> "~/"        --> current user (slash added) 
~user/a/b/c.txt     --> "~user/"    --> named user 
~user               --> "~user/"    --> named user (slash added)

The output will be the same irrespective of the machine that the code is running on. ie. both Unix and Windows prefixes are matched regardless.
[中]从完整文件名获取前缀,例如C:/~/
此方法将处理Unix或Windows格式的文件。前缀包括完整文件名中的第一个斜杠(如果适用)。

Windows: 
a\b\c.txt           --> ""          --> relative 
\a\b\c.txt          --> "\"         --> current drive absolute 
C:a\b\c.txt         --> "C:"        --> drive relative 
C:\a\b\c.txt        --> "C:\"       --> absolute 
\\server\a\b\c.txt  --> "\\server\" --> UNC 
Unix: 
a/b/c.txt           --> ""          --> relative 
/a/b/c.txt          --> "/"         --> absolute 
~/a/b/c.txt         --> "~/"        --> current user 
~                   --> "~/"        --> current user (slash added) 
~user/a/b/c.txt     --> "~user/"    --> named user 
~user               --> "~user/"    --> named user (slash added)

无论代码在哪台机器上运行,输出都是相同的。Unix和Windows前缀都是匹配的。

代码示例

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

public static boolean isNormalizedPathOutsideWorkingDir(String path) {
    final String normalize = FilenameUtils.normalize(path);
    final String prefix = FilenameUtils.getPrefix(normalize);
    return (normalize != null && StringUtils.isBlank(prefix));
  }
}

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

return getPrefix(filename);  // add end slash if necessary
} else {
  return filename;

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

/**
 * Does the work of getting the path.
 * 
 * @param filename  the filename
 * @param includeSeparator  true to include the end separator
 * @return the path
 */
private static String doGetFullPath(String filename, boolean includeSeparator) {
  if (filename == null) {
    return null;
  }
  int prefix = getPrefixLength(filename);
  if (prefix < 0) {
    return null;
  }
  if (prefix >= filename.length()) {
    if (includeSeparator) {
      return getPrefix(filename);  // add end slash if necessary
    } else {
      return filename;
    }
  }
  int index = indexOfLastSeparator(filename);
  if (index < 0) {
    return filename.substring(0, prefix);
  }
  int end = index + (includeSeparator ?  1 : 0);
  return filename.substring(0, end);
}

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

@Test
public void testGetPrefix() {
  assertEquals(null, FilenameUtils.getPrefix(null));
  assertEquals(null, FilenameUtils.getPrefix(":"));
  assertEquals(null, FilenameUtils.getPrefix("1:\\a\\b\\c.txt"));
  assertEquals(null, FilenameUtils.getPrefix("1:"));
  assertEquals(null, FilenameUtils.getPrefix("1:a"));
  assertEquals(null, FilenameUtils.getPrefix("\\\\\\a\\b\\c.txt"));
  assertEquals(null, FilenameUtils.getPrefix("\\\\a"));
  assertEquals("", FilenameUtils.getPrefix(""));
  assertEquals("\\", FilenameUtils.getPrefix("\\"));
  assertEquals("C:", FilenameUtils.getPrefix("C:"));
  assertEquals("C:\\", FilenameUtils.getPrefix("C:\\"));
  assertEquals("//server/", FilenameUtils.getPrefix("//server/"));
  assertEquals("~/", FilenameUtils.getPrefix("~"));
  assertEquals("~/", FilenameUtils.getPrefix("~/"));
  assertEquals("~user/", FilenameUtils.getPrefix("~user"));
  assertEquals("~user/", FilenameUtils.getPrefix("~user/"));
  assertEquals("", FilenameUtils.getPrefix("a\\b\\c.txt"));
  assertEquals("\\", FilenameUtils.getPrefix("\\a\\b\\c.txt"));
  assertEquals("C:\\", FilenameUtils.getPrefix("C:\\a\\b\\c.txt"));
  assertEquals("\\\\server\\", FilenameUtils.getPrefix("\\\\server\\a\\b\\c.txt"));
  assertEquals("", FilenameUtils.getPrefix("a/b/c.txt"));
  assertEquals("/", FilenameUtils.getPrefix("/a/b/c.txt"));
  assertEquals("C:/", FilenameUtils.getPrefix("C:/a/b/c.txt"));
  assertEquals("//server/", FilenameUtils.getPrefix("//server/a/b/c.txt"));
  assertEquals("~/", FilenameUtils.getPrefix("~/a/b/c.txt"));

代码示例来源:origin: iterate-ch/cyberduck

private String parent(final String absolute) {
  final String prefix = FilenameUtils.getPrefix(absolute);
  if(absolute.equals(prefix)) {
    return null;
  }
  int index = absolute.length() - 1;
  if(absolute.charAt(index) == this.getDelimiter()) {
    if(index > 0) {
      index--;
    }
  }
  final int cut = absolute.lastIndexOf(this.getDelimiter(), index);
  if(cut > FilenameUtils.getPrefixLength(absolute)) {
    return absolute.substring(0, cut);
  }
  return String.valueOf(prefix);
}

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

@Test
public void testGetPrefix_with_nullbyte() {
  try {
    assertEquals("~user\\", FilenameUtils.getPrefix("~u\u0000ser\\a\\b\\c.txt"));
  } catch (final IllegalArgumentException ignore) {
  }
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.commons-io

/**
 * Does the work of getting the path.
 * 
 * @param filename  the filename
 * @param includeSeparator  true to include the end separator
 * @return the path
 */
private static String doGetFullPath(String filename, boolean includeSeparator) {
  if (filename == null) {
    return null;
  }
  int prefix = getPrefixLength(filename);
  if (prefix < 0) {
    return null;
  }
  if (prefix >= filename.length()) {
    if (includeSeparator) {
      return getPrefix(filename);  // add end slash if necessary
    } else {
      return filename;
    }
  }
  int index = indexOfLastSeparator(filename);
  if (index < 0) {
    return filename.substring(0, prefix);
  }
  int end = index + (includeSeparator ?  1 : 0);
  return filename.substring(0, end);
}

代码示例来源:origin: pentaho/pentaho-platform

/**
 * Gets the prefix from a full filename.
 * <p/>
 * The prefix includes the first slash in the full filename where applicable.
 * 
 * <pre>
 * a/b/c.txt           --> ""          --> relative
 * /a/b/c.txt          --> "/"         --> absolute
 * </pre>
 * <p/>
 * 
 * @param filename
 *          the filename to query, null returns null
 * @return the prefix of the file, null if invalid
 */
public static String getPrefix( final String filename ) {
 return FilenameUtils.getPrefix( filename );
}

代码示例来源:origin: org.apache.commons/com.springsource.org.apache.commons.io

/**
 * Does the work of getting the path.
 * 
 * @param filename  the filename
 * @param includeSeparator  true to include the end separator
 * @return the path
 */
private static String doGetFullPath(String filename, boolean includeSeparator) {
  if (filename == null) {
    return null;
  }
  int prefix = getPrefixLength(filename);
  if (prefix < 0) {
    return null;
  }
  if (prefix >= filename.length()) {
    if (includeSeparator) {
      return getPrefix(filename);  // add end slash if necessary
    } else {
      return filename;
    }
  }
  int index = indexOfLastSeparator(filename);
  if (index < 0) {
    return filename.substring(0, prefix);
  }
  int end = index + (includeSeparator ?  1 : 0);
  return filename.substring(0, end);
}

代码示例来源:origin: classmethod/gradle-aws-plugin

private String createKey(String name, Object version, String prefix) {
  String path = name.substring(FilenameUtils.getPrefix(name).length());
  String baseName = FilenameUtils.getBaseName(name);
  String extension = FilenameUtils.getExtension(name);
  return String.format(Locale.ENGLISH, "%s/%s/%s-%s-%s%s", new Object[] {
    prefix,
    path,
    baseName,
    version,
    createTimestamp(),
    extension.length() > 0 ? "." + extension : ""
  });
}

代码示例来源:origin: io.github.stephenc.docker/docker-client-shaded

return getPrefix(filename);  // add end slash if necessary
} else {
  return filename;

代码示例来源:origin: org.geoserver.web/gs-web-core

public Object getDisplayValue(File f) {
  if (f == USER_HOME) {
    return new ParamResourceModel("userHome", component).getString();
  } else {
    GeoServerResourceLoader loader =
        GeoServerExtensions.bean(GeoServerResourceLoader.class);
    if (f.equals(loader.getBaseDirectory())) {
      return new ParamResourceModel("dataDirectory", component).getString();
    }
  }
  try {
    final String displayName =
        FileSystemView.getFileSystemView().getSystemDisplayName(f);
    if (displayName != null && !displayName.trim().isEmpty()) {
      return displayName.trim();
    }
    return FilenameUtils.getPrefix(f.getAbsolutePath());
  } catch (Exception e) {
    LOGGER.log(
        Level.FINE,
        "Failed to get file display name, "
            + "on Windows this might be related to a known java bug http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6973685",
        e);
    // on windows we can get the occasional NPE due to
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6973685
  }
  return f.getName();
}

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

return getPrefix(filename);  // add end slash if necessary
} else {
  return filename;

代码示例来源:origin: org.artifactory/artifactory-web-common

private RootsDropDownChoice(String id) {
    super(id);
    if (chRoot != null) {
      // allow only chRoot
      ArrayList<File> files = new ArrayList<>(1);
      File rootFile = new File(chRoot);
      files.add(rootFile);
      setChoices(files);
      setChoiceRenderer(new RootOnlyChoiceRenderer());
      setDefaultModel(new Model<>(rootFile));
    } else {
      // allow changing root
      List<File> roots = Arrays.asList(File.listRoots());
      setChoices(roots);
      setVisible(!roots.isEmpty());
      File defaultRoot = new File(FilenameUtils.getPrefix(getCurrentFolder())).getAbsoluteFile();
      setDefaultModel(new Model<>(defaultRoot));
    }
    add(new AjaxFormComponentUpdatingBehavior("onchange") {
      @Override
      protected void onUpdate(AjaxRequestTarget target) {
        File root = getModelObject();
        setCurrentFolder(root.getAbsolutePath());
        FileBrowserPanel.this.setDefaultModelObject(root);
        target.add(FileBrowserPanel.this);
      }
    });
  }
}

代码示例来源:origin: org.kie.commons/kie-nio2-model

return getPrefix(filename);  // add end slash if necessary
} else {
  return filename;

代码示例来源:origin: org.geoserver.community/gs-geogig

@Override
public Object getDisplayValue(File f) {
  if (f == USER_HOME) {
    return new ParamResourceModel("userHome", DirectoryChooser.this).getString();
  } else {
    GeoServerResourceLoader loader =
        GeoServerExtensions.bean(GeoServerResourceLoader.class);
    if (f.equals(loader.getBaseDirectory())) {
      return new ParamResourceModel("dataDirectory", DirectoryChooser.this)
          .getString();
    }
  }
  try {
    final String displayName =
        FileSystemView.getFileSystemView().getSystemDisplayName(f);
    if (displayName != null && displayName.length() > 0) {
      return displayName;
    }
    return FilenameUtils.getPrefix(f.getAbsolutePath());
  } catch (Exception e) {
    // on windows we can get the occasional NPE due to
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6973685
  }
  return f.getName();
}

代码示例来源:origin: org.onosproject/onlab-thirdparty

return getPrefix(filename);  // add end slash if necessary
} else {
  return filename;

代码示例来源:origin: iterate-ch/cyberduck

/**
 * @param directory Parent directory
 * @return True if this is a child in the path hierarchy of the argument passed
 */
public boolean isChild(final Local directory) {
  if(this.isRoot()) {
    // Root cannot be a child of any other path
    return false;
  }
  if(Objects.equals(this.parent(this.getAbsolute()), this.parent(directory.getAbsolute()))) {
    // Cannot be a child if the same parent
    return false;
  }
  final String prefix = FilenameUtils.getPrefix(this.getAbsolute());
  String parent = this.getAbsolute();
  while(!parent.equals(prefix)) {
    parent = this.parent(parent);
    if(directory.getAbsolute().equals(parent)) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: org.uberfire/vfs-model

return getPrefix(filename);  // add end slash if necessary
} else {
  return filename;

代码示例来源:origin: Nextdoor/bender

return getPrefix(filename);  // add end slash if necessary
} else {
  return filename;

相关文章