获取文件夹名以及文件名
public static void main(String[] args) throws Exception {
String path = "D:\\upload\\loan";
java.util.List<String> stringList = getFiles(path);
for(String s:stringList){
System.out.println(s);
}
System.out.println("获取第一级文件夹名------END-----------------------------------");
for(String s:stringList){
System.out.println("一级文件夹名----"+s);
String path1 = "D:\\upload\\loan\\"+s;
java.util.List<String> stringList1 = getFiles(path1);
for(String s1:stringList1){
System.out.println("二级文件夹名----"+s1);
}
}
System.out.println("获取第一级和第二级文件夹名------END-----------------------------------");
for(String s:stringList){
System.out.println("一级文件夹名----"+s);
String path1 = "D:\\upload\\loan\\"+s;
java.util.List<String> stringList1 = getFiles(path1);
for(String s1:stringList1){
System.out.println("二级文件夹名----"+s1);
String path2 = path1 +"\\"+ s1;
java.util.List<String> stringList2 = getFiless(path2);
for(String s2:stringList2){
System.out.println("照片名------"+s2);
}
}
}
}
public static java.util.List<String> getFiles(String path){
File file = new File(path);
File[] array = file.listFiles();
java.util.List<String> stringList = new ArrayList<>();
for(int i=0;i<array.length;i++){
if(array[i].isDirectory()){
stringList.add(array[i].getName());
}
}
return stringList;
}
public static java.util.List<String> getFiless(String path){
File file = new File(path);
File[] array = file.listFiles();
java.util.List<String> stringList = new ArrayList<>();
for(int i=0;i<array.length;i++){
if(array[i].isFile()){
stringList.add(array[i].getName());
}
}
return stringList;
}
实现效果
移动文件
public static void main(String[] args) throws Exception {
String from = "D:\\upload\\loan\\10001\\证件照\\";
String to = "D:\\upload\\loan\\10001\\催收照片\\";
fileMove(from,to);
}
public static void fileMove(String from, String to){
try {
File dir = new File(from);
File[] files = dir.listFiles();
if (files == null) return;
File moveDir = new File(to);
if (!moveDir.exists()) {
moveDir.mkdirs();
}
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
fileMove(files[i].getPath()
,to + "\\" + files[i].getName());
files[i].delete();
}
File moveFile =
new File(moveDir.getPath() + "\\"
+ files[i].getName());
if (moveFile.exists()) {
moveFile.delete();
}
files[i].renameTo(moveFile);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("移动文件>>>>"+e.getMessage());
}
System.out.println("【移动文件结束】>>>>>>"+DateUtils.getTime());
}
原文件夹图片
移动后
|