基本上,我的文件夹中有一些称为模式的图像。所有图像均为png
文件格式。
以下是我正在使用的代码:
import java.io.File;
public class IMG_List {
public static void main(String [] args){
File file = new File("C:/images/Patterns");
String[] str = file.list();
for(String f_name : str){
String[] str_name = f_name.split(".");
System.out.println(str_name[0]);
}
}
}
当我使用上面的代码时,我得到:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at IMG_List.main(IMG_List.java:11)
但是,当我使用以下代码时,我没有收到错误
import java.io.File;
public class IMG_List {
public static void main(String [] args){
File file = new File("C:/images/Patterns");
String[] str = file.list();
for(String f_name : str){
String[] str_name = f_name.split("png");
System.out.println(str_name[0]);
}
}
}
为什么我不使用点将字符串分开?
谢谢,MMK。
您必须在点之前使用转义字符,才能将其重新表示为regexp,因为split
函数接受regexp
public String[] split(String regex)
\\.
在正则表达式中使用以表示点,因为这.
意味着正则表达式中的任何字符
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句