jmate 0.4 is out
Posted by
afsina
on Saturday, November 29, 2008
/
Comments: (0)
So i decided to release 0.4 version of jmate. it has some minor additions in it, nothing serious. You can download it here: http://code.google.com/p/jmate/ . Changes can be seen here: http://code.google.com/p/jmate/source/list (i am too lazy to write a changelog now.). i removed some more classes. it is still around 70kb. i used the library in another small project of ours tr-spell, you can check how it reduces the code amount when dealing with files especially . Such as WordListGenerator class.
Helpers to the rescue
Posted by
afsina
on Sunday, August 17, 2008
/
Comments: (3)
Whenever a scripting language wants to make a good appearance, they compare themselves with Java with deliberately verbose code snippets. Reading a file, some regular expression tricks, or even a Hello World application.
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
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.
Iterate through a text file's lines.
Get a text file's lines in an enhanced for loop
Write a string as a file
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
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")
.encoding("utf-8")
.filters(StringFilters.PASS_ONLY_TEXT)
.trim()
.build()
.asStringList();
Iterate through a text file's lines.
LineIterator li = new SimpleFileReader("foo.txt").getLineIterator();
while (li.hasNext())
out.println(li.next());
IOs.closeSilently(li);
Get a text file's lines in an enhanced for loop
for (String s : new SimpleFileReader("foo.txt").getIterableReader())
out.println(s);
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.
new SimpleFileWriter
.Builder("foo.txt")
.encoding("utf-8")
.keepOpen(true)
.build()
.writeLines(Collects.newArrayList("Hello", "World"));
Save a web page as a file
new SimpleFileWriter("foo.txt").copyFromURL("http://google.com");
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 File("pages"),
new Files.ExtensionFilter("jsp", "tag"));