1、写一个实体类:imageName,这里用到ArrayList实体类进行排序,因此定义一个类
package zero.file.videoProject.bean.util; public class imageName { private String name; private int num; public imageName() { } public imageName(String name,int num){ this.name = name; this.num = num; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } @Override public String toString() { return "imageName{" + "name='" + name + '\'' + ", num=" + num + '}'; } }
2、用正则表达式提取,ArrayList进行排序,
package zero.file.videoProject.util.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import zero.file.videoProject.bean.Student; import zero.file.videoProject.bean.util.imageName; import zero.file.videoProject.bean.video; import zero.file.videoProject.mappers.untils.tonameMapper; import java.io.File; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @RunWith(SpringRunner.class) @SpringBootTest public class UserMapperTest { @Autowired public tonameMapper tonamejpg; @Test public void testInsert(){ Pattern pattern = Pattern.compile("(.+(?=\\.mp4))+"); Pattern pattern2 = Pattern.compile("^\\d+"); Scanner sc = new Scanner(System.in); System.out.println("请输入要处理的文件所在的目录:"); String folederPath = sc.nextLine(); File f = new File(folederPath); // File f = new File("C:\\Users\\17517\\Videos\\bilibili\\Java"); File[] fileArray = f.listFiles(); String name = null; String name2 = null; List<Integer> list = new ArrayList<Integer>(); ArrayList<imageName> imageName = new ArrayList<imageName>(); Map<String,Integer> map = new HashMap<>(); video video = new video(); for (int i = 0; i <= fileArray.length - 1; i++) { Matcher matcher = pattern.matcher(fileArray[i].getName()); while (matcher.find()) { name = matcher.group(); Matcher matcher2 = pattern2.matcher(name); while (matcher2.find()) { name2 = matcher2.group(); int num = Integer.valueOf(name2); imageName s1 = new imageName(name,num); imageName.add(s1); map.put(name,num); } } } Collections.sort(imageName, new Comparator<imageName>() { @Override public int compare(imageName o1, imageName o2) { int num = o1.getNum() - o2.getNum(); return num; } }); for (imageName s : imageName){ System.out.println(s.getName() + "," + s.getNum()); video.setId(s.getName()); video.setCategory(s.getName()); video.setName(s.getName()); video.setLabel(s.getName()); tonamejpg.insert(video); } } }
效果:用输入文件所在目录
3、就复制好路径,粘贴过去就导入到数据库中了