I decided to write a quick Java program to do this instead of messing around with scripting languages or a Linux bash shell.
It worked very well, and I am rather pleased with the result and Java 8.
It contains the following "new/newer/not-very-old" stuff:
- a lambda
- a stream (of Strings)
- a method reference (used as a lambda)
- the java.nio.file package (New IO)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.nio.file.FileSystems; | |
import java.nio.file.Files; | |
import java.io.IOException; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
public class ParseTextFile | |
{ | |
public static void process(String url) { | |
try | |
{ | |
String filename = url.replace("http://","").replace("https://",""); | |
int index = filename.indexOf("/"); | |
filename = filename.substring(index+1); | |
Path path = Paths.get(filename); | |
Files.createDirectories(path.getParent()); | |
Files.move(path.getFileName(), path); | |
System.out.println(filename); | |
} | |
catch (IOException e) | |
{ | |
// e.printStackTrace(); | |
throw new RuntimeException(e); | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
Files.lines(FileSystems.getDefault().getPath("list.txt")).forEach(ParseTextFile::process); | |
} | |
} |
References
- [1] Java SE 8 - Official Javadoc
- https://docs.oracle.com/javase/8/docs/api/
No comments:
Post a Comment