Get file extension name in Java

Description

The following code shows how to get file extension name.

Example


/*from w  ww.  j av a 2s  .co  m*/
import java.io.File;

public class Main {
  public static void main(String[] argv) {
    getFileExtensionName(new File("a.txt"));
  }

  public static String getFileExtensionName(File f) {
    if (f.getName().indexOf(".") == -1) {
      return "";
    } else {
      return f.getName().substring(f.getName().length() - 3, f.getName().length());
    }
  }
}




















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip