File renaming and moving operations
Sometimes in order to access and manage files in a unified manner, it is necessary to rename the files and move them to a new folder. How to implement them?
A simple java applet can be implemented:
part_1:Requirements: I need to rename the video files under all subfolders in the <(E:/BaiduYun/Chuanzhi Podcast_Zhang Xiaoxiang_Java Multithreading and Concurrency Library Advanced Application Video Tutorial Download)> folder and move them to a new location <(E:/BaiduYun/Zhang Xiaoxiang_Java Multithreading and Concurrency Library)>;
part_2: The directory structure is as follows:
E:/BaiduYun
E:/BaiduYun/Chuanzhi Podcast_Zhang Xiaoxiang_Download advanced application video tutorial for Java multithreading and concurrency library
E:/BaiduYun/Chuanzhi Podcast Zhang Xiaoxiang_Java multi-threading and concurrency library advanced application video tutorial download/01Chuanzhi Podcast Zhang Xiaoxiang's traditional threading technology review
part_3: Program source code + comments:
package cn.mike.demo;import java.io.File;import java.io.FileNotFoundException;import java.util.ArrayList;import java.util.List;/** * @author Administrator * @usage This program implements file renaming and moving operations; */public class RenameFiles {private static File srcFolder;// Source folder private static File destFolder;// Destination folder private static List<File> srcFiles;// Store all files to be named static {srcFolder = new File("E://BaiduYun//Chuanzhi Podcast_Zhang Xiaoxiang_Java Multithreading and Concurrency Library Advanced Application Video Tutorial Download");destFolder = new File("E://BaiduYun//Zhang Xiaoxiang_Java Multithreading and Concurrency Library");srcFiles = new ArrayList<File>();}public static void main(String[] args) {// Verify the legality of the folder (whether it exists) try {checkFolder();}catch (FileNotFoundException e) {e.printStackTrace();return;}// traverse the source folder and put the file to be modified into the collection iterateGetFiles(RenameFiles.srcFolder);// Rename the elements in the collection (and move to the target folder) iterateRename();}// end method-mainprivate static void checkFolder() throws FileNotFoundException {if (!RenameFiles.srcFolder.exists()) {throw new FileNotFoundException("The specified source folder does not exist.");}if (!RenameFiles.destFolder.exists()) {throw new FileNotFoundException("The specified target folder does not exist.");}} private static void iterateRename() {String aviName = null;String tempStr = null;StringBuilder strBuilder = new StringBuilder();File tempFile = null;String sequenceNumber = null;String detailName = null;// traverse the list collection and rename it one by one for (File each: RenameFiles.srcFiles) {aviName = each.getName().substring(0, each.getName().length() - 4);// Get the file name (excluding the suffix name ".avi")tempStr = each.getParent();// The name of the parent folder sequenceNumber = String.format("%02d", Integer.valueOf(aviName));// The sequence number of two digits, less than two digits are filled with 0, for example: 01detailName = tempStr.substring(tempStr.lastIndexOf("_") + 1);// The detailed content of the video file, for example: traditional thread mutual exclusion technology strBuilder.append(sequenceNumber + "_" + detailName + ".avi");tempFile = new File(RenameFiles.destFolder, strBuilder.toString());// path of the new file// each.renameTo(tempFile);// core code (rename and move) System.out.println(tempFile.toString());// Print to the console for debugging strBuilder.delete(0, strBuilder.length());// Remember to clear strBuilder}// end foreach}// end method-iterateRenameprivate static void iterateGetFiles(File srcFile) {// If it is a folder, continue to traverse in depth if (srcFile.isDirectory()) {File[] files = srcFile.listFiles();for (File each : files) {iterateGetFiles(each);}} else if (srcFile.getAbsolutePath().endsWith(".avi")) {// If it is not a folder and the file format is avi, add the file to the list collection of the files to be named RenameFiles.srcFiles.add(srcFile);}}// end method-iterateGetFiles}// end class-RenameFilespart_4: The effect after renaming and moving:
E:/BaiduYun/Zhang Xiaoxiang_Java multithreading and concurrency library
Summarize
The above is the entire content of this article about the renaming and moving operation instance code of Java files. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!