Java implements methods for renaming files

  • 2020-04-01 04:01:03
  • OfStack

This article illustrates how to rename a Java implementation file. Share with you for your reference. The details are as follows:

Downloaded movies always have some useless information such as website names and so on as an ocd patient must delete them


package sys.file;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
public class ZReName {
 public static void main(String args[]) {
  ZReName r = new ZReName();
  r.replace();
  //r.changeOrder();
 }
 public void replace (){
  File dir = new File("G:// The movie // That year, that rabbit thing [ The original version ]");
  //Here is the folder path for the hair replacement, using a double slash
  String[] files = dir.list();
  File f = null;
  String filename = ""; 
  //String oldName = "[movie paradise www.dygod.cn]";
  String oldName = ""; //The part of the name to be replaced
  String newName = ""; //The name to be replaced with, is empty is deleted
  for (String file : files) {
   f = new File(dir, file);
   //And notice, you have to write "File" (fl, File) if you don't want to write "File", you have to have the full path
   filename = f.getName();
   System.out.println(filename);
   String S1= "\d{4}.( youku | Sohu video )-";
   ZReName r = new ZReName();
   boolean b = r.regex1(S1, filename);
   oldName = r.regex(S1, filename);
   if (b){
    //f.renameTo(new File(fl.getAbsolutePath()+"//"+filename.replace(" The content to replace "," Replace with the content ")));
    //You can use replace over and over again, or you can use regular expressions
    // You can use replace over and over again, or you can use regular expressions
    f.renameTo(new File(dir.getAbsolutePath() + "//"+ filename.replace(oldName, newName)));
    //Delete the first X bits
    //f.renameTo(new File(dir.getAbsolutePath() + "//"+ filename.substring(9)));
   }
  }
  System.exit(0);
 }
 public void changeOrder (){
  File dir = new File("G://Merger ");
  //Here is the folder path for the hair replacement, using a double slash
  String[] files = dir.list();
  File f = null;
  String filename = ""; 
  //String oldName = "[movie paradise www.dygod.cn]";
  String oldName = ""; //The part of the name to be replaced
  String newName = ""; //The name to be replaced with, is empty is deleted
  for (String file : files) {
   f = new File(dir, file);
   //And notice, you have to write "File" (fl, File) if you don't want to write "File", you have to have the full path
   filename = f.getName();
   System.out.println(filename);
   String S1= "\d+";
   ZReName r = new ZReName();
   boolean b = r.regex1(S1, filename);
   oldName = filename;
   newName = r.regex(S1, filename)+" "+filename;
   if (b){
    //f.renameTo(new File(fl.getAbsolutePath()+"//"+filename.replace(" The content to replace "," Replace with the content ")));//You can use replace over and over again, or you can use regular expressions
    // You can use replace over and over again, or you can use regular expressions
    f.renameTo(new File(dir.getAbsolutePath() + "//"+ filename.replace(oldName, newName)));
    //Delete the first X bits
    //f.renameTo(new File(dir.getAbsolutePath() + "//"+ filename.substring(9)));
   }
  }
  System.exit(0);
 }
 //The regular expression S1 is looking for, and S2 is looking for the source
 public String regex(String S1,String S2){
  Pattern p = Pattern.compile(S1);
  Matcher m = p.matcher(S2);
  boolean result = m.find();
  //System.out.println(result);
  String S ="";
  if (result) {
   S = m.group();
   System.out.println(S);
  }
  return S;
 }
 //The regular expression S1 is looking for, and S2 is looking for the source
 public boolean regex1(String S1,String S2){
  Pattern p = Pattern.compile(S1);
  Matcher m = p.matcher(S2);
  boolean result = m.find();
  System.out.println(result);
  return result;
 }
}

I hope this article has been helpful to your Java programming.


Related articles: