[Java] A quiz that batch converts a file name delimited by a specific character string with a part of the file name

2 minute read

Background

Environment

-I do it on Mac

$ java -version
openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.1+7)
Eclipse OpenJ9 VM AdoptOpenJDK (build openj9-0.20.0, JRE 14 Mac OS X amd64-64-Bit Compressed References 20200416_40 (JIT enabled, AOT enabled)
OpenJ9-05fa2d361
OMR-d4365f371
JCL-5757187cae based on jdk-14.0.1+7)

Advance preparation

  • Similarly, the original article is abc_test_ok_1_this.csv…``abc_test_ok_100_this.csv’’ is prepared as a one-liner from the place where the file is prepared.
echo'IntStream.range(1,101).forEach(i -> (try (Files.createFile(Paths.get("abc_test_ok_"+String.valueOf(i) + "_this.csv"),java.nio.file.attribute .PosixFilePermissions.asFileAttribute(java.nio.file.attribute.PosixFilePermissions.fromString("r--r--r--")));} catch (Exception e) {throw new RuntimeException(e);}})' | jshell-
  • It’s easy to think that one liner is not suitable for Java because it requires compilation, but recent Java can do it because JShell called REPL tool can be used.
  • While writing with echo, I am passing to the jshell command with a pipe. The last-is to prevent extra display.
  • The import statement can be omitted depending on the item.
  • The way of writing permissions is based on Unix, so I don’t think it works on Windows.
  • I thought it would be a loss to write a loop with a For statement, so I used forEach from Stream without any particular meaning.
  • If you write with a line break, it will be as follows. Exception handling is a hassle.
        IntStream.range(1,101).forEach(i -> {
            try {
                Files.createFile(Paths.get("abc_test_ok_"+String.valueOf(i) + "_this.csv"),
                    PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("r--r--r--")));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });

Rename file

  • Here is the main issue.
  • , but suddenly it moved below.

echo'Path path1 = Paths.get(".");try{Files.list(path1).forEach(from -> {String intStr = from.toString().replaceAll("[^0-9]", " ");try {Files.move(from, Paths.get(".",intStr + ".csv"));} catch (IOException e) {e.printStackTrace();}});} catch(IOException e ) {e.printStackTrace();}' | jshell-
  • Since only the numbers are taken in the regular expression and renamed to the file name, it may not work if there are extra files in the folder.
  • After taking the Path of the current directory, I immediately take the one directly under list and turn it with forEach, and rename the file with move.
  • If you write with a line break, it will be as follows. After all, exception handling seems to be troublesome. (It is meaningless that the post-processing of exception handling is different from the above)
  • (Added on 7/15) Refactored to use lambda expressions.
        Path path1 = Paths.get(".");
        try{
Files.list(path1).forEach(from -> {
                String intStr = from.toString().replaceAll("[^0-9]", "");
                try {
                    Files.move(from, Paths.get(".",intStr + ".csv"));
} catch (IOException e) {
                    e.printStackTrace();
                }
            });
        } catch(IOException e) {e.printStackTrace();}
}

in conclusion

  • I touched JShell after a long time. It is good to move quickly.
  • I did not do file processing much like the last person, so I did it while checking, so please point out any better way.