Java examples for Language Basics:String
Use the endsWith() method on a given file name.
public class Main { /*from www . j a v a 2 s .c om*/ public static void main(String[] args) { String filename = "test.java"; System.out.println("Finding file type of: " + filename); if(filename.endsWith(".txt")){ System.out.println("Text file"); } else if (filename.endsWith(".doc")){ System.out.println("Document file"); } else if (filename.endsWith(".xls")){ System.out.println("Excel file"); } else if (filename.endsWith(".java")){ System.out.println("Java source file"); } else { System.out.println("Other type of file"); } } }