Moved nasty Java IO handling into new IoUtils object and factored it more nicely.