Java indeed is not a new language and not really suitable for some scripting tasks by default. However, sometimes for such tasks (or for any task actually) you don't have time to learn a script language, it may be handy to use helper libraries. Actually that is the spirit of Java, that if something can be reused, make it a small library instead of doing everything with core language or libraries over and over.
There are several good helper libraries available. Like apache commons, or Google collections. but commons suffer from age and fragmentation. Google collections is modern but may not be suitable for small applications. So i decided to mix and match (In other words I stole code form those libs), and write a little bit by myself for some of our applications. My aim is to keep the library size always less than 100kb. it may have a lot of flaws, bu so far it worked for me. i call the project jmate, project code is here..
Nothing really fancy, but as a result you can write such code now:
Read a UTF-8 file as a String
String content = new SimpleFileReader("foo.txt", "utf-8").asString();Read a file as a String List
List<String> lst = new SimpleFileReader("foo.txt").asStringList();
Read a utf-8 file as a string list, but skip the non-text lines. trim the lines. Regular expression filters also can be built.
List<String> lst = new SimpleFileReader.Builder("foo.txt")
Iterate through a text file's lines.
LineIterator li = new SimpleFileReader("foo.txt").getLineIterator();
Get a text file's lines in an enhanced for loop
for (String s : new SimpleFileReader("foo.txt").getIterableReader())
Write a string as a file
new SimpleFileWriter(tmpFile).writeString("Hello World!");
write a String list as a utf-8 file, but keep the writer open after the writeLines() call. By default SimpleFileWriter closes the stream after any of its method calls.
Save a web page as a file
Get all jsp, tag files in a directory and sub directories
List<File> files = Files.crawlDirectory(There are more to write but it is the basic idea. neither API nor the code is stabilized. but if you are interested you can check the code. Beware.. There is shameless copying form Google collections and Apache Commons libraries.
new Files.ExtensionFilter("jsp", "tag"));