Get file size and file extension in JAVA

1. Get the file extension

public static String getExtensionName(String filename) {     

  if ((filename != null) && (filename.length() > 0)) {     

    int dot = filename.lastIndexOf('.');     

    if ((dot >-1) && (dot < (filename.length() - 1))) {     

      return filename.substring(dot + 1);     

            }     

        }     

  return filename;     

}     

2. Get the file name without extension

public static String getFileNameNoEx(String filename) {     

  if ((filename != null) && (filename.length() > 0)) {     

    int dot = filename.lastIndexOf('.');     

    if ((dot >-1) && (dot < (filename.length()))) {     

      return filename.substring(0, dot);     

         }     

      }     

  return filename;     

}

3. Get the file size

String path = "PATH"; //fill in the file path here

File f = new File(path);

int size = new FileInputStream(f).available() / 1024 / 1024; //The result is in M

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324672569&siteId=291194637