判断指定目录下是否有后缀名为.jpg的文件,如果有,就输出该文件名称
/*
判断指定目录下是否有后缀名为.jpg的文件,如果有,就输出该文件名称
思路:
1.将用list()方法获取目录下的各文件名(含后缀)
2.for循环遍历String数组中文件名
3.查找.最后一次出现的索引位置、找出当前字符串的最大下标
4.根据substring(int beginIndex, int endIndex)从文件名中截取除.jpg
5.与.jpg比较,true则存在,并输出该文件名称
*/
@Test
public void test(){
File file = new File("D:\\Program Files (x86)\\Programming software\\algorithmsAndDataStructuresCode\\Test111\\IO\\io");
//1.将用list()方法获取目录下的各文件名(含后缀)
String[] list = file.list();
//2.for循环遍历String数组中文件名
for (String str : list) {
//3.查找.最后一次出现的索引位置、找出当前字符串的最大下标
int lastIndexOf = str.lastIndexOf(".");
int lastLength = str.length();
//4.根据substring(int beginIndex, int endIndex)从文件名中截取除.jpg
//其中是截取到endIndex索引的前一个
String substring = str.substring(lastIndexOf, lastLength);
//5.与.jpg比较,true则存在,并输出该文件名称
if(substring.equals(".jpg")){
System.out.println(str);
}
}
}