在文件扩展名之前为文件名添加后缀

Adding a suffix to a file name before file extension

我需要创建一个应用程序,在文件扩展名之前为某个文件名添加一些后缀。 该应用程序采用一个强制性参数 - 配置文件的路径。 我用 value/key:

做了一个 config.properties 文件

mode=copy(保留源文件) suffix=abc files=change.txt:further.txt:yandex.txt(后缀文件列表。文件路径以列:.分隔)

为了记录消息,我使用 java.util.logging

我做了以下代码:

    public class Suffixing {
        public static String PATH_TO_PROPERTIES = "src/main/resources/config.properties";
    
        public static void main(String[] args) throws IOException {
    
            FileInputStream fileInputStream;
            Logger logging = Logger.getLogger(Suffixing.class.getName());
            Properties prop = new Properties();
    
            fileInputStream = new FileInputStream(PATH_TO_PROPERTIES);
            prop.load(fileInputStream);
            
            String mode = prop.getProperty("mode");
            String suffix = prop.getProperty("suffix");
            String files = prop.getProperty("files");

            File one = new File("D:/map/test/change.txt");
            File two = new File("D:/map/test/further.txt");
            File three = new File("D:/map/test/yandex.txt");
            for (String file : files.split(":")) {
    
                if (mode != "copy") {
                    logging.log(Level.SEVERE, "Mode is not recognized: " + mode);
                    fileInputStream.close();
                } else if (prop.getProperty(suffix) == "") {
                    logging.log(Level.SEVERE, "No suffix is configured");
                    fileInputStream.close();
                } else if (prop.getProperty("files") == "") {
                    logging.log(Level.WARNING, "No files are configured to be copied/moved");
                    fileInputStream.close();
                } else if (!one.exists() || !two.exists() || !three.exists()) {
                    logging.log(Level.SEVERE, "No such file: ");
                } else {
    
                    int at = file.indexOf('.');
                    String newFile = file.substring(0, at) + suffix + file.substring(at);
                    File dest = new File("D:/map/test" + newFile);
                    logging.log(Level.INFO, prop.getProperty("files") + "-->" + dest);
                }
            }
    
    
        }
    }

对于某些输入,代码运行不正确,我需要修改什么?

不要将 String== 进行比较。您应该始终使用 equals() 代替。在 How do I compare strings in Java?.

阅读更多内容

话虽如此,请尝试以下操作:

public class Suffixing {
    public static String PATH_TO_PROPERTIES = "src/main/resources/config.properties";

    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream;
        Logger logging = Logger.getLogger(Suffixing.class.getName());
        Properties prop = new Properties();

        fileInputStream = new FileInputStream(PATH_TO_PROPERTIES);
        prop.load(fileInputStream);
        
        String mode = prop.getProperty("mode");
        String suffix = prop.getProperty("suffix");
        String files = prop.getProperty("files");

        File one = new File("D:/map/test/change.txt");
        File two = new File("D:/map/test/further.txt");
        File three = new File("D:/map/test/yandex.txt");
        for (String file : files.split(":")) {
            if (!Objects.equals(mode, "copy")) {
                logging.log(Level.SEVERE, "Mode is not recognized: " + mode);
                fileInputStream.close();
            } else if (prop.getProperty(suffix).isBlank()) {
                logging.log(Level.SEVERE, "No suffix is configured");
                fileInputStream.close();
            } else if (prop.getProperty("files").isBlank()) {
                logging.log(Level.WARNING, "No files are configured to be copied/moved");
                fileInputStream.close();
            } else if (!one.exists() || !two.exists() || !three.exists()) {
                logging.log(Level.SEVERE, "No such file: ");
            } else {
                int at = file.indexOf('.');
                String newFile = file.substring(0, at) + suffix + file.substring(at);
                File dest = new File("D:/map/test" + newFile);
                logging.log(Level.INFO, prop.getProperty("files") + "-->" + dest);
            }
        }
    }
}

如果您想在第一次出错时停止执行,您最好像这样更改所有错误日志记录:

logging.log(Level.SEVERE, "Mode is not recognized: " + mode);

抛出 RuntimeException(或您选择的任何合适的异常 class),例如:

throw new RuntimeException("Mode is not recognized: " + mode);

你应该整理你的文件处理,干净地加载属性:

Properties prop = new Properties();
try(var in = new FileInputStream(PATH_TO_PROPERTIES)) {
    prop.load(in);
}
String mode = prop.getProperty("mode");
String suffix = prop.getProperty("suffix");
String files = prop.getProperty("files");

在循环内进行所有这些检查是没有意义的,因为除非设置了“文件”,否则它们不会被执行,因此将它们从 for 循环中拉出来:

if (!Objects.equals(mode, "copy"))
    logging.log(Level.SEVERE, "Mode is not recognized: " + mode);
if (suffix == null || suffix.isBlank()) {
    logging.log(Level.SEVERE, "No suffix is configured");
}
if (files == null || files.isBlank()) {
    logging.log(Level.WARNING, "No files are configured to be copied/moved");
}

不清楚为什么需要 one/two/three 和这一行,如果文件是从 config.properties:

读取的
if (!one.exists() || !two.exists() || !three.exists()) {
     logging.log(Level.SEVERE, "No such file: ");

“文件”的循环应该使用 lastIndexOf() 以防目录名称中有点。

for (String file : files.split(":")) {
    File f = new File(file);
    if (!f.exists()) {
        logging.log(Level.SEVERE, "No such file: "+f);
    } else {
        int at = file.lastIndexOf('.');
        String newFile = file.substring(0, at) + "-" + suffix + file.substring(at);
        File dest = new File(newFile);
        logging.log(Level.INFO, f + "-->" + dest);
    }
}

split(":") 不适用于带有驱动器号的 Windows 路径,但您已在另一个 .

中讨论了该问题