在文件扩展名之前为文件名添加后缀
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
。
- 如果无法识别来自配置的模式,则记录
Mode is not recognized: <mod_input_value>
严重级别并停止执行。
示例:Mode is not recognized: copi
- 如果未设置后缀,则记录
No suffix is configured
严重级别并停止执行。
- 如果未指定文件,记录
No files are configured to be copied/moved
警告级别并停止执行。
- 如果指定文件之一不存在,则记录
No such file: <file-path>
严重级别,但不要停止其他文件的处理。请注意,消息中的文件路径不能包含反斜杠,而必须包含正斜杠 ('/') 以分隔路径部分。
示例:No such file: src/test/resources/no-such-file.txt
- 复制文件时,在 INFO 级别记录
<source-file> -> <destination-file>
。
请注意,消息中的文件路径不能包含反斜杠,而必须包含正斜杠 (/) 以分隔路径部分,而不是反斜杠。
示例:src/test/resources/file.txt -> src/test/resources/file-suffix.txt
我做了以下代码:
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 路径,但您已在另一个 .
中讨论了该问题
我需要创建一个应用程序,在文件扩展名之前为某个文件名添加一些后缀。
该应用程序采用一个强制性参数 - 配置文件的路径。
我用 value/key:
config.properties
文件
mode=copy
(保留源文件)
suffix=abc
files=change.txt:further.txt:yandex.txt
(后缀文件列表。文件路径以列:.分隔)
为了记录消息,我使用 java.util.logging
。
- 如果无法识别来自配置的模式,则记录
Mode is not recognized: <mod_input_value>
严重级别并停止执行。 示例:Mode is not recognized: copi
- 如果未设置后缀,则记录
No suffix is configured
严重级别并停止执行。 - 如果未指定文件,记录
No files are configured to be copied/moved
警告级别并停止执行。 - 如果指定文件之一不存在,则记录
No such file: <file-path>
严重级别,但不要停止其他文件的处理。请注意,消息中的文件路径不能包含反斜杠,而必须包含正斜杠 ('/') 以分隔路径部分。 示例:No such file: src/test/resources/no-such-file.txt
- 复制文件时,在 INFO 级别记录
<source-file> -> <destination-file>
。 请注意,消息中的文件路径不能包含反斜杠,而必须包含正斜杠 (/) 以分隔路径部分,而不是反斜杠。 示例:src/test/resources/file.txt -> src/test/resources/file-suffix.txt
我做了以下代码:
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 路径,但您已在另一个