Get Extension File Java Code Example

Snippet 1

      private static String getFileExtension(File file) {
        String extension = "";
 
        try {
            if (file != null && file.exists()) {
                String name = file.getName();
                extension = name.substring(name.lastIndexOf("."));
            }
        } catch (Exception e) {
            extension = "";
        }
 
        return extension;
 
    } 

Snippet 2

  import java.util.LinkedList;
import java.io.File;
public class getFileExtention{
 public static String[] getExt(File path, String extentionType) {
  String [] g = path.list();
  //We need to convert it into a linkedlist for 
        //adding whatever we want any size
  //You could also do it with a number
  //and iterate over it 
  LinkedList h = new LinkedList();
  for(int c = 0;c < g.length;c++) {
   if(g[c].endsWith(extentionType)) {
    h.add(g[c]);
   }
  }
  String[] j = new String[h.size()];
  //Covert it into a array again 
  //but not if you made it into a linked list type method
  for (int d = 0; d < h.size(); d++) {
   j[d] = h.get(d);
  }
  return j;
 }
} 

Similar Snippets


Creating Java Main Method Code Example - java

Find Duplicates In Arraylist Java Code Example - java

Java Creat A Folder Code Example - java

Firestore Find Doc And Set Data Code Example - java

Create Copy Of Array From Another Array Code Example - java

Copyright © Code Fetcher 2020

 

 

Published

Leave a comment

Your email address will not be published. Required fields are marked *