如何使用Java硒将文本追加到现有文件名中。
示例: 当前文件名:19062306.csv
New filename : 19062306 ABC.csv
package package1;
import java.io.File;
import java.util.Arrays;
import org.apache.commons.io.FilenameUtils;
public class Rename {
public static void main(String[] args) {
File f = null;
String[] paths;
try {
f = new File("E:\\HCA_Automation\\Files");
paths = f.list();
for(String path:paths) {
System.out.println(path);
String[] array = path.split(".");
System.out.println("input string: " + path);
System.out.println("output array after splitting with . : " + Arrays.toString(array));
array = path.split("\\.");
System.out.println("input string: " + path);
System.out.println("output array after splitting with regex'\\.' : " + Arrays.toString(array));
array = path.split("[.]");
System.out.println("input string: " + path);
System.out.println("output array after splitting with regex '[.]' : " + Arrays.toString(array));
String filename = array[0];
String extension = array[1];
System.out.println("file: " + path);
System.out.println("name: " + filename);
System.out.println("extension: " + extension);
//String base = FilenameUtils.removeExtension(filename);
//extension = FilenameUtils.getExtension(filename);
String result = filename + " PAS" + "." +extension;
System.out.println(result);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
当我尝试执行上述代码时,在执行SOP时会重命名文件,但是当我在文件夹中检入文件时,文件尚未重命名。有人可以帮我吗?
答案 0 :(得分:2)
这是简单的Java编程,与Selenium无关:
.
字符的位置。或使用String::split
。