get File Name Without Extension, Retrieve the main file name. - Android java.io

Android examples for java.io:File Name

Description

get File Name Without Extension, Retrieve the main file name.

Demo Code

import java.io.File;

import android.text.TextUtils;

public class Main {

  /**//from ww w. j a v a2s .  c o m
   * Retrieve the main file name.
   * 
   * @param path
   *          the file name.
   * @return the main file name without the extension.
   */
  public static String getFileNameWithoutExtension(final String path) {
    if (TextUtils.isEmpty(path)) {
      return null;
    }
    return getFileNameWithoutExtension(new File(path));
  }

  /**
   * Retrieve the main file name.
   * 
   * @param file
   *          the file.
   * @return the main file name without the extension.
   */
  public static String getFileNameWithoutExtension(final File file) {
    if (null == file) {
      return null;
    }
    String fileName = file.getName();
    final int index = fileName.lastIndexOf('.');
    if (index >= 0) {
      fileName = fileName.substring(0, index);
    }
    return fileName;
  }

}

Related Tutorials