Add index to filename for existing file (file.txt => file_1.txt)

后端 未结 4 780
孤城傲影
孤城傲影 2021-01-23 05:18

I want to add an index to a filename if the file already exists, so that I don\'t overwrite it.

Like if I have a file myfile.txt and same time myfile.

4条回答
  •  后悔当初
    2021-01-23 05:48

    Try this link partly answers your query.

    https://stackoverflow.com/a/805504/1961652

        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setIncludes(new String[]{"**/myfile*.txt"});
        scanner.setBasedir("C:/Temp");
        scanner.setCaseSensitive(false);
        scanner.scan();
        String[] files = scanner.getIncludedFiles();
    

    once you have got the correct set of files, append a proper suffix to create a new file.

提交回复
热议问题