Java File Extension Name Extract getFileExtension(File file)

Here you can find the source of getFileExtension(File file)

Description

get File Extension

License

Open Source License

Declaration

public static String getFileExtension(File file) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {
    public static String getFileExtension(File file) {
        return getFileExtension(file.getPath());
    }//ww  w  .j  a v a 2  s  .  c o m

    public static String getFileExtension(String path) {
        int dot = indexOfDot(path);
        if (dot < 0) {
            return null;
        } else {
            return path.substring(dot + 1);
        }
    }

    private static int indexOfDot(String path) {
        int baseNameStart = path.lastIndexOf(File.separatorChar);
        if (baseNameStart < 0) {
            baseNameStart = 0;
        } else {
            ++baseNameStart;
        }

        int dot = -1;

        int pos = path.length();
        while ((pos = path.lastIndexOf('.', pos - 1)) > baseNameStart) {
            dot = pos;
        }

        return dot;
    }
}

Related

  1. extractFileExtension(String path)
  2. extractFileExtensionFromPath(String path)
  3. getFileExtension(File f)
  4. getFileExtension(File file)
  5. getFileExtension(File file)
  6. getFileExtension(File file)
  7. getFileExtension(File file)
  8. getFileExtension(File file)
  9. getFileExtension(File file)