How to generate a file name as needed?

I was offered this code to solve one of my problems:

private static final String PATH_FMT = 
    "C:\\Users\\{0}\\Desktop\\Bloomberg Rechnung-{1,date,yyyyMMdd}{2,choice,0< ({2})}.xlsx";

private File save() throws IOException {
    Date now = new Date();
    for (int fCounter = 0; ; fCounter++) {
        Path path = Paths.get(
            MessageFormat.format(PATH_FMT, this.username, now, fCounter)
        );
        try (OutputStream out = Files.newOutputStream(path, CREATE_NEW)) {
            this.wb.write(out);
            this.wb.close();
            return path.toFile();
        } catch (FileAlreadyExistsException incrCounterAndRetry) {
        }
    }
}

Unfortunately, this code does not fully fulfill what I want. He should create a file. The first file that it should create without another file with the name should not have a counter. The second file that is created must have a counter in the file name (no matter where it starts). I am currently getting the file with the counter 0 as the first file. Can someone help me fix this problem? And maybe someone can explain to me how it works {2,choice,0< ({2})}.

Thanks in advance

+4
source share
1 answer

Give it a try {2,choice,0#|0<{2,number,integer}}.

To explain every part

  • 2 - ,
  • choice - ,
  • 0# - , ,
  • | -
  • 0< -
  • {2,number,integer} - 2, .
+5

Source: https://habr.com/ru/post/1663076/


All Articles