[Practice] Use NotePad ++ to write java programs that add file name suffixes in batches

[Practice] Use NotePad ++ to write java programs that add file name suffixes in batches

  • NotePad ++ uses UTF-8 encoding

 

 

  • Open cmd and compile the .java file: javac -encoding UTF-8 ***. Java

 

  • Run .class: java ***

 

  • code show as below:
 1 import java.io.File;
 2 import java.util.regex.*;
 3 
 4 public class Main {
 5 
 6     public static void main(String[] args) {
 7         String[] fileset = new String[]{"15","16"};
 8         for (int i = 0; i < fileset.length; i++){
 9             String filepath = fileset[i];
10             rename(filepath);
11         }
12         
13     }
14     
15     
16      public  static  void rename (String path) {
 17  
18          // Folder 
19          File folder = new File (path);
 20          System.out.println (folder.getAbsolutePath ());
 21          // Whether the folder exists 
22          if ( ! folder.exists ()) {
 23              System.out.println ("error" );
 24          }
 25  
26          // Get the files in the folder 
27          File [] files = folder.listFiles ();
 28          
29          for ( int i = 0; i <files.length; i ++) {
30             
31             if (files[i].isFile()) {
32                 //数字的正则表达式
33                 Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");  
34                 String filename = files[i].getName();
35                 if (pattern.matcher(filename).matches()){
36                     files[i].renameTo(new File(path+"//"+filename+".jpg"));
37                 }
38                 
39             }
40             
41         }
42     }
43 
44 }

 

Guess you like

Origin www.cnblogs.com/landiljy/p/12716200.html