Here you can find the source of getFilename(File file)
Parameter | Description |
---|---|
file | is a file such as <code>/home/data/a.dat</code> |
public static String getFilename(File file)
//package com.java2s; /*//from w w w.j av a 2 s . co m * License information at https://github.com/Caltech-IPAC/firefly/blob/master/License.txt */ import java.io.*; public class Main { /** * return filename without the path. * * @param file is a file such as <code>/home/data/a.dat</code> * @return File the filename only. i.e. if "/home/data/abc.dat" is * passed to this method it will return "abc.dat" */ public static String getFilename(File file) { String ext = getExtension(file); String base = getBase(file); return setExtension(ext, base); } /** * Get the extension of a filename. * @param s a file name such as <code>a.dat</code> * @return String the extension of the file. * A null is returned if there is no extension; * */ public static String getExtension(String s) { String ext = null; int i = s.lastIndexOf('.'); if (i > 0 && i < s.length() - 1) { ext = s.substring(i + 1).toLowerCase(); } return ext; } /** * Get the extension of a file. * @param f a file such as <code>a.dat</code> * @return String the extension of the file. * A null is returned if there is no extension * */ public static String getExtension(File f) { return getExtension(f.getName()); } /** * Get the name of of a filename without the extension. * @param s a file name such as <code>a.dat</code> * @return String the base name of the file. i.e. if "abc.dat" is * passed to this method it will return "abc" * A null is returned if there is no base; * */ public static String getBase(String s) { String base; int i = s.lastIndexOf('.'); if (i == -1 || i == 0) { base = s; } else { base = s.substring(0, i); } return base; } /** * Get the name of of a filename without the extension. * @param f a file name such as <code>a.dat</code> * @return String the base name of the file. i.e. if "abc.dat" is * passed to this method it will return "abc" * A null is returned if there is no base; * */ public static String getBase(File f) { return getBase(f.getName()); } /** * Set the extension of a filename. * @param extensionName the extension to be replaced or added. * @param fileName a file name such as <code>xxxx.dat</code> or <code>xxx</code> * @param replace if true replace the filename extension (if it has one) with the specified extension, otherwise just add the extension to the filename. * @return String The file with the specified extension. */ public static String setExtension(String extensionName, String fileName, boolean replace) { int dotPosition = fileName.lastIndexOf("."); String newFileName; //AR253: TLau 08/02/10 no extension to set if extensionName is null. if (extensionName == null) return fileName; if (dotPosition == -1) { newFileName = fileName + "." + extensionName; } else if (replace) { newFileName = fileName.substring(0, dotPosition + 1) + extensionName; } else { // has extension && !replace newFileName = fileName + "." + extensionName; } return newFileName; } public static File setExtension(String extensionName, File fileName, boolean replace) { return new File(setExtension(extensionName, fileName.getPath(), replace)); } /** * Set the extension of a filename. If the file already has * an extension the replace the extension with the specified * one. * @param extensionName a file name such as <code>xxxx.dat</code> or <code>xxx</code> * @param fileName the extension to be replaced or added. * @return String The file with the specified extension. */ public static String setExtension(String extensionName, String fileName) { return setExtension(extensionName, fileName, true); } }