--I saw the following article
-\
--I'm doing it on a 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)
--Similarly, the original article is done with one liner from the place where the file of ʻabc_test_ok_1_this.csv`` ・ ・ ・
ʻabc_test_ok_100_this.csv`` is prepared, so from there
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 is easy to think that Java is not suitable for one-liner because it requires compilation, but in recent Java, you can use a REPL tool called JShell. --While writing with echo, I'm passing it to the jshell command via a pipe. The last-is to prevent unnecessary display. --The import statement can be omitted depending on the item. ――Since the way to write permissions is Unix-based, I don't think it will work on Windows. ――I thought I would lose if I wrote the loop in a For statement, so I used forEach from Stream without any particular meaning. --If you write it with line breaks, 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);
}
});
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 with regular expressions 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, you can quickly take the one directly under the list and turn it with forEach, and rename the file with move. --If you write it with line breaks, 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) -(7/15 postscript) Refactored to use lambda expression.
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();}
}
――I touched JShell for the first time in a long time. It's good to move quickly. ――As with the previous person, I don't usually do much file processing, so I did it while investigating, so please point out if there is a better way.
Recommended Posts