获取目录下所有文件
package com.croot.rims;
import com.croot.rims.dao.AttachmentinfoDao;
import com.croot.rims.dao.ProjectMapper;
import com.croot.rims.entity.Attachmentinfo;
import com.croot.rims.entity.story.request.MappingFiles;
import com.croot.rims.entity.story.request.StoreFiles;
import com.croot.rims.service.story.StoryService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RimsServerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SynchronizeAttachmentsTask {
@Test
public void test() {
String basePath = "C:\\Users\\Administrator\\Desktop\\fsdownload";
File dir = new File(basePath);
List<File> allFileList = new ArrayList<>();
// 判断文件夹是否存在
if (!dir.exists()) {
System.out.println("目录不存在");
return;
}
getAllFile(dir, allFileList);
for (File file : allFileList) {
String fileName = file.getName();
System.out.println(fileName);
}
System.out.println("该文件夹下共有" + allFileList.size() + "个文件");
}
public static void getAllFile(File fileInput, List<File> allFileList) {
// 获取文件列表
File[] fileList = fileInput.listFiles();
assert fileList != null;
for (File file : fileList) {
if (file.isDirectory()) {
// 递归处理文件夹
// 如果不想统计子文件夹则可以将下一行注释掉
getAllFile(file, allFileList);
} else {
// 如果是文件则将其加入到文件数组中
allFileList.add(file);
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65