复制 file1 名称代替 file2 并向其添加额外内容,但不复制 file1 扩展名

Copy file1 name inplace of file2 and add extra to it, but not copy file1 extention

如何更改文件名以匹配原始文件并在其中包含额外的 txt,请参见下面的示例

原创 dataFile.txt thisNameNeedsToMatchDataFile.txt(但在 .txt 之前也添加了“.Step2”)

期望的输出 dataFile.txt 数据文件.Step2.txt

如有任何帮助,请提前致谢。

我如何理解你的问题这就是我认为解决方案的样子 (java 7+)

public class FileConverter {

public static void main(String[] args) throws IOException {
    FileConverter converter = new FileConverter();
    File newFile1 = new File("c:/parent1/file1.dump");
    File newFile2 = new File("c:/parent2/file2.dump");
    converter.convert2Files(newFile1, newFile2);
}

private void convert2Files(File file1, File file2) throws IOException {
    // compare if the 2 files have the same name
    if(file1.getName().equalsIgnoreCase(file2.getName())){
        String newName = file1.getName().substring(0, file1.getName().lastIndexOf('.')); 
        String newNameOriginal = newName + ".txt";
        String newNameStep2 = newName + ".Step2.txt";
        File newFileOriginal = new File(file1.getParent(),newNameOriginal);
        File newFileStep2 = new File(file1.getParent(),newNameStep2);
        Path file = file1.toPath();/* use file1 as source file */
        Path to = Paths.get(file1.getParent());/* path to destination directory */
        Files.copy(file, to.resolve(newFileOriginal.toPath()));
        Files.copy(file, to.resolve(newFileStep2.toPath()));
    }
}
}