在我的应用程序,当我们浏览文件从SD卡的文件将被.text区段,jpg和mpeg4ie视频文件。我想保存每个文件类型到一个特定的文件夹。例如,文件的.text到文本文件夹。我的问题是,当我选择文件,我怎么检查的文件扩展名?
In my application, when we browse the file from sdcard the file will be .text, .jpg and mpeg4ie video file. I want to store each file type into a particular folder. For example, .text files go to the text folder. My question is when I select the file, how do I check the file extension?
我会得到的文件名作为字符串,将其分割成一个数组。作为分隔符,然后拿到阵列,这将是文件扩展名的最后一个索引。例如:
I would get the file name as a String, split it into an array with "." as the delimiter, and then get the last index of the array, which would be the file extension. For example:
public class main {
public static void main(String[] args) {
String filename = "image.jpg";
String filenameArray[] = filename.split("\\.");
String extension = filenameArray[filenameArray.length-1];
System.out.println(extension);
}
}
它输出:
jpg