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

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

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

FilenameUtils.removeExtension介绍

[英]Removes the extension from a filename.

This method returns the textual part of the filename before the last dot. There must be no directory separator after the dot.

foo.txt    --> foo 
a\b\c.jpg  --> a\b\c 
a\b\c      --> a\b\c 
a.b\c      --> a.b\c

The output will be the same irrespective of the machine that the code is running on.
[中]从文件名中删除扩展名。
此方法返回文件名最后一个点之前的文本部分。点后面不能有目录分隔符。

foo.txt    --> foo 
a\b\c.jpg  --> a\b\c 
a\b\c      --> a\b\c 
a.b\c      --> a.b\c

无论代码在哪台机器上运行,输出都是相同的。

代码示例

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

/**
 * Gets the base name, minus the full path and extension, from a full filename.
 * <p>
 * This method will handle a file in either Unix or Windows format.
 * The text after the last forward or backslash and before the last dot is returned.
 * <pre>
 * a/b/c.txt --&gt; c
 * a.txt     --&gt; a
 * a/b/c     --&gt; c
 * a/b/c/    --&gt; ""
 * </pre>
 * <p>
 * The output will be the same irrespective of the machine that the code is running on.
 *
 * @param filename  the filename to query, null returns null
 * @return the name of the file without the path, or an empty string if none exists. Null bytes inside string
 * will be removed
 */
public static String getBaseName(final String filename) {
  return removeExtension(getName(filename));
}

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

/**
 * Gets the base name, minus the full path and extension, from a full filename.
 * <p>
 * This method will handle a file in either Unix or Windows format.
 * The text after the last forward or backslash and before the last dot is returned.
 * <pre>
 * a/b/c.txt --> c
 * a.txt     --> a
 * a/b/c     --> c
 * a/b/c/    --> ""
 * </pre>
 * <p>
 * The output will be the same irrespective of the machine that the code is running on.
 *
 * @param filename  the filename to query, null returns null
 * @return the name of the file without the path, or an empty string if none exists
 */
public static String getBaseName(String filename) {
  return removeExtension(getName(filename));
}

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

@Test
public void testRemoveExtension() {
  assertEquals(null, FilenameUtils.removeExtension(null));
  assertEquals("file", FilenameUtils.removeExtension("file.ext"));
  assertEquals("README", FilenameUtils.removeExtension("README"));
  assertEquals("domain.dot", FilenameUtils.removeExtension("domain.dot.com"));
  assertEquals("image", FilenameUtils.removeExtension("image.jpeg"));
  assertEquals("a.b/c", FilenameUtils.removeExtension("a.b/c"));
  assertEquals("a.b/c", FilenameUtils.removeExtension("a.b/c.txt"));
  assertEquals("a/b/c", FilenameUtils.removeExtension("a/b/c"));
  assertEquals("a.b\\c", FilenameUtils.removeExtension("a.b\\c"));
  assertEquals("a.b\\c", FilenameUtils.removeExtension("a.b\\c.txt"));
  assertEquals("a\\b\\c", FilenameUtils.removeExtension("a\\b\\c"));
  assertEquals("C:\\temp\\foo.bar\\README", FilenameUtils.removeExtension("C:\\temp\\foo.bar\\README"));
  assertEquals("../filename", FilenameUtils.removeExtension("../filename.ext"));
}

代码示例来源:origin: apache/hive

destFileName = FilenameUtils.removeExtension(destFileName) + "-" + sha
  + FilenameUtils.EXTENSION_SEPARATOR + FilenameUtils.getExtension(destFileName);

代码示例来源:origin: apache/hive

destFileName = FilenameUtils.removeExtension(destFileName) + "-" + sha
  + FilenameUtils.EXTENSION_SEPARATOR
  + FilenameUtils.getExtension(destFileName);

代码示例来源:origin: apache/drill

destFileName = FilenameUtils.removeExtension(destFileName) + "-" + sha
  + FilenameUtils.EXTENSION_SEPARATOR + FilenameUtils.getExtension(destFileName);

代码示例来源:origin: apache/drill

destFileName = FilenameUtils.removeExtension(destFileName) + "-" + sha
  + FilenameUtils.EXTENSION_SEPARATOR
  + FilenameUtils.getExtension(destFileName);

代码示例来源:origin: ata4/disunity

public SerializedFileReader(Path file) throws IOException {
  String fileName = file.getFileName().toString();
  String fileExt = FilenameUtils.getExtension(fileName);
  // load audio buffer if existing
  readResourceStream(file.resolveSibling(fileName + ".streamingResourceImage"));
  readResourceStream(file.resolveSibling(fileName + ".resS"));
  // join split serialized files before loading
  if (fileExt.startsWith("split")) {
    L.fine("Found split serialized file");
    fileName = FilenameUtils.removeExtension(fileName);
    List<Path> parts = new ArrayList<>();
    int splitIndex = 0;
    // collect all files with .split0 to .splitN extension
    while (true) {
      String splitName = String.format("%s.split%d", fileName, splitIndex);
      Path part = file.resolveSibling(splitName);
      if (Files.notExists(part)) {
        break;
      }
      L.log(Level.FINE, "Adding splinter {0}", part.getFileName());
      splitIndex++;
      parts.add(part);
    }
    // load all parts to one byte buffer
    in = DataReaders.forByteBuffer(ByteBufferUtils.load(parts));
  } else {
    in = DataReaders.forFile(file, READ);
  }
}

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

/**
   * Command line interface to the vocal tract linear scaler effect.
   * 
   * @param args
   *            the command line arguments. Exactly two arguments are expected: (1) the factor by which to scale the vocal tract
   *            (between 0.25 = very long and 4.0 = very short vocal tract); (2) the filename of the wav file to modify. Will
   *            produce a file basename_factor.wav, where basename is the filename without the extension.
   * @throws Exception
   *             if processing fails for some reason.
   */
  public static void main(String[] args) throws Exception {
    if (args.length != 2) {
      System.err.println("Usage: java " + VocalTractLinearScalerEffect.class.getName() + " <factor> <filename>");
      System.exit(1);
    }
    float factor = Float.parseFloat(args[0]);
    String filename = args[1];
    AudioDoubleDataSource input = new AudioDoubleDataSource(AudioSystem.getAudioInputStream(new File(filename)));
    AudioFormat format = input.getAudioFormat();
    VocalTractLinearScalerEffect effect = new VocalTractLinearScalerEffect((int) format.getSampleRate());
    DoubleDataSource output = effect.apply(input, "amount:" + factor);
    DDSAudioInputStream audioOut = new DDSAudioInputStream(output, format);
    String outFilename = FilenameUtils.removeExtension(filename) + "_" + factor + ".wav";
    AudioSystem.write(audioOut, AudioFileFormat.Type.WAVE, new File(outFilename));
    System.out.println("Created file " + outFilename);
  }
}

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

/**
   * Command line interface to the vocal tract linear scaler effect.
   * 
   * @param args
   *            the command line arguments. Exactly two arguments are expected: (1) the factor by which to scale the vocal tract
   *            (between 0.25 = very long and 4.0 = very short vocal tract); (2) the filename of the wav file to modify. Will
   *            produce a file basename_factor.wav, where basename is the filename without the extension.
   * @throws Exception
   *             if processing fails for some reason.
   */
  public static void main(String[] args) throws Exception {
    if (args.length != 2) {
      System.err.println("Usage: java " + VocalTractLinearScalerEffect.class.getName() + " <factor> <filename>");
      System.exit(1);
    }
    float factor = Float.parseFloat(args[0]);
    String filename = args[1];
    AudioDoubleDataSource input = new AudioDoubleDataSource(AudioSystem.getAudioInputStream(new File(filename)));
    AudioFormat format = input.getAudioFormat();
    VocalTractLinearScalerEffect effect = new VocalTractLinearScalerEffect((int) format.getSampleRate());
    DoubleDataSource output = effect.apply(input, "amount:" + factor);
    DDSAudioInputStream audioOut = new DDSAudioInputStream(output, format);
    String outFilename = FilenameUtils.removeExtension(filename) + "_" + factor + ".wav";
    AudioSystem.write(audioOut, AudioFileFormat.Type.WAVE, new File(outFilename));
    System.out.println("Created file " + outFilename);
  }
}

代码示例来源:origin: jeremylong/DependencyCheck

final String fileName = FilenameUtils.removeExtension(f.getName());
final String ext = FilenameUtils.getExtension(f.getName());
if (!IGNORED_FILES.accept(f) && !"js".equals(ext)) {

代码示例来源:origin: jeremylong/DependencyCheck

pomProperties = retrievePomProperties(path, jar);
} else {
  path = FilenameUtils.removeExtension(dependency.getActualFilePath()) + ".pom";
  pomFile = new File(path);

代码示例来源:origin: mulesoft/mule

private String getName(String application) {
 return removeExtension(FilenameUtils.getName(application));
}

代码示例来源:origin: winterDroid/android-drawable-importer-intellij-plugin

public static String getExportNameFromFilename(String filename) {
  String exportName = FilenameUtils.removeExtension(filename);
  if (exportName.matches("[a-z0-9_.]*")) {
    return exportName;
  }
  exportName = exportName.toLowerCase();
  return exportName.replaceAll("([^(a-z0-9_.)])", "_");
}

代码示例来源:origin: bonigarcia/webdrivermanager

protected File postDownload(File archive) {
  File parentFolder = archive.getParentFile();
  File[] ls = parentFolder.listFiles();
  for (File f : ls) {
    if (getDriverName().contains(removeExtension(f.getName()))) {
      log.trace("Found binary in post-download: {}", f);
      return f;
    }
  }
  throw new WebDriverManagerException("Driver " + getDriverName()
      + " not found (using temporal folder " + parentFolder + ")");
}

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

/**
   * Return supportFiles (if found) for the specified file
   *
   * @param filePath
   * @return
   */
  public List<File> getSupportFiles(String filePath) {
    List<File> supportFiles = null;
    String parent = FilenameUtils.getFullPath(filePath);
    String mainName = FilenameUtils.getName(filePath);
    String baseName = FilenameUtils.removeExtension(mainName);
    for (String extension : supportingExtensions) {
      String newFilePath = parent + baseName + extension;
      File file = new File(newFilePath);
      if (file.exists()) {
        if (supportFiles == null) {
          supportFiles = new ArrayList<File>();
        }
        supportFiles.add(file);
      }
    }
    return supportFiles;
  }
}

代码示例来源:origin: winterDroid/android-drawable-importer-intellij-plugin

@Override
  protected void setValue(Object o) {
    File file = (File) o;
    if (file == null) {
      setText("");
      return;
    }
    if (file.isDirectory()) {
      setText(file.getAbsolutePath());
    } else {
      setText(FilenameUtils.removeExtension(file.getName()));
    }
  }
};

代码示例来源:origin: openmrs/openmrs-core

/**
 * Returns a {@link File} for the given obs complex data to be written to. The output file
 * location is determined off of the {@link OpenmrsConstants#GLOBAL_PROPERTY_COMPLEX_OBS_DIR}
 * and the file name is determined off the current obs.getComplexData().getTitle().
 * 
 * @param obs the Obs with a non-null complex data on it
 * @return File that the complex data should be written to
 */
public File getOutputFileToWrite(Obs obs) throws IOException {
  String title = obs.getComplexData().getTitle();
  String titleWithoutExtension = FilenameUtils.removeExtension(title);
  String extension = "." + StringUtils.defaultIfEmpty(FilenameUtils.getExtension(title), "dat");
  String uuid = obs.getUuid();
  String filename;
  
  if (StringUtils.isNotBlank(titleWithoutExtension)) {
    filename = titleWithoutExtension + "_" + uuid + extension;
  } else {
    filename = uuid + extension;
  }
  
  File dir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(
    Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR));
  File outputfile = new File(dir, filename);
  
  return outputfile;
}

代码示例来源:origin: openmrs/openmrs-core

@Test
public void getOutputFileToWrite_shouldCorrectlyNameTitledFileWithoutExtension() throws IOException, ParseException {
  ComplexData complexDataWithoutExtension = new ComplexData(FilenameUtils.removeExtension(FILENAME), null);
  
  Obs obsWithoutExtension = new Obs();
  obsWithoutExtension.setComplexData(complexDataWithoutExtension);
  
  File extensionlessFile = handler.getOutputFileToWrite(obsWithoutExtension);
  extensionlessFile.createNewFile();
  
  String[] nameWithoutExtension = extensionlessFile.getName().split("_|\\.");
  
  String titlePartExtensionless = nameWithoutExtension[0];
  String uuidPartExtensionless = nameWithoutExtension[1];
  String extensionPartExtensionless = nameWithoutExtension[2];
  
  assertEquals(titlePartExtensionless, FilenameUtils.removeExtension(FILENAME));
  assertEquals(extensionPartExtensionless, "dat");
  assertEquals(uuidPartExtensionless, obsWithoutExtension.getUuid());
}

代码示例来源:origin: openmrs/openmrs-core

@Test
public void getOutputFileToWrite_shouldCorrectlyNameTitledFileWithExtension() throws IOException, ParseException {
  ComplexData complexDataWithTitle = new ComplexData(FILENAME, null);
  
  Obs obsWithTitle = new Obs();
  obsWithTitle.setComplexData(complexDataWithTitle);
  
  File titledFile = handler.getOutputFileToWrite(obsWithTitle);
  titledFile.createNewFile();
  
  String[] nameWithTitle = titledFile.getName().split("_|\\.");
  
  String titlePart = nameWithTitle[0];
  String uuidPartWithTitle = nameWithTitle[1];
  String extensionPart = nameWithTitle[2];
  
  assertEquals(titlePart, FilenameUtils.removeExtension(FILENAME));
  assertEquals(extensionPart, "txt");
  assertEquals(uuidPartWithTitle, obsWithTitle.getUuid());
}

相关文章