1 读取某个目录下所有文件、文件夹
public static ArrayList<String>
getFiles(String path) {
ArrayList<String> files =
new ArrayList<String>();
File file =
new File(path);
File[] tempList = file.listFiles();
for (
int i =
0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
files.add(tempList[i].toString());
}
if (tempList[i].isDirectory()) {
}
}
return files;
}
2 3种从文件路径中获取文件名的方法
package test;
import java.io.File;
public class FileName {
/**
* @param args
*/
public static void main(String[] args) {
String fName =
" G:\\Java_Source\\navigation_tigra_menu\\demo1\\img\\lev1_arrow.gif ";
File tempFile =
new File( fName.trim());
String fileName = tempFile.getName();
System.out.println(
"fileName = " + fileName);
String fName = fName.trim();
String fileName = fName.substring(fName.lastIndexOf(
"/")+
1);
String fileName = fName.substring(fName.lastIndexOf(
"\\")+
1);
System.out.println(
"fileName = " + fileName);
String fName = fName.trim();
String temp[] = fName.split(
"\\\\");
/**split里面必须是正则表达式,"\\"的作用是对字符串转义*/
String fileName = temp[temp.length-
1];
System.out.println(
"fileName = " + fileName);
}
}