Hacker News new | past | comments | ask | show | jobs | submit login

My first impression is that this guy is 10 years late!

And I agree that it's funny for a Java programmer to be able to read a file with open('bleh').read() instead of that 15 stream buffer stream reader stream whatever line of code.




I never really understood how the developers and maintainers of the Java language could put up with the overly complicated disaster that is file I/O. They've managed to take something that's relatively simple in most languages and turn it into an utter disaster. I was shocked, years ago, when I was learning Java after having lived in C++ world.


...and if you're processing text files line by line, it isn't even necessary to call read() or readlines() on the file, since a file object supports iteration. You could say:

    for line in open("todo.txt"):
        # do something with line


You could also

    [ do_something_to(line) for line in open('todo.txt') ]


Although you'd probably want to replace those square brackets with parens to make it a generator, if you're dealing with a largish file.


Indeed. Hope it's not a huge file.


Default Java File I/O is pretty horrible but like a lot of things about Java, the widely-used Apache Commons makes it better:

String fileContents = FileUtils.readFileToString(new File("blah"));

Still not quite as concise as Ruby/Python/etc though.


Surely late is better than never.


I am admittedly late to the party. I went to a university where they taught Java (and unfortunately not Python). For the last 10 years, Java has paid the bills, and it still does, so I'm not knocking Java. I stuck with it for so long because I felt that it took that much real world usage to get a good grip on the language.

But you are right --- I'm definitely not the first developer to have had that "moment of zen" when opening files in Python, but hopefully I'm also not the last.


So welcome :) In a couple of months, if you really want to have that "moment of zen" again, give a chance to SICP (it's a wonderful book) which will teach you functional programming and Scheme in the same time.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: