zoukankan      html  css  js  c++  java
  • 【实践】使用NotePad++编写批量添加文件名后缀的java程序

    【实践】使用NotePad++编写批量添加文件名后缀的java程序

    • 安装jdk,windows系统需要配置环境变量
    • NotePad++使用UTF-8编码

    • 打开cmd,编译.java文件:javac -encoding UTF-8 ***.java
    • 运行.class:java ***
    • 代码如下:
     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         //文件夹
    19         File folder = new File(path);
    20         System.out.println(folder.getAbsolutePath());
    21         //文件夹是否存在
    22         if(!folder.exists()){
    23             System.out.println("error");
    24         }
    25 
    26         //获取文件夹中的文件
    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 }
  • 相关阅读:
    SpringBoot整合阿里云OSS
    UVALive
    HDU 5794 A Simple Chess dp+Lucas
    数论
    UVALive
    UVALive
    HDU 5792 World is Exploding 树状数组+枚举
    UVALive
    UVALive
    UVALive
  • 原文地址:https://www.cnblogs.com/landiljy/p/12716200.html
Copyright © 2011-2022 走看看