Everyone enjoys a good language bashing now and then--especially when it's a shining example of how the language makes it hard to do something amazingly simple, like reading a file.

Posted by jzawodn at July 02, 2003 08:58 PM

Reader Comments
# Arcterex said:

open FILE, "file.txt";
@lines = <FILE>;

... and that's the *long* way to do it :) What can I say, I'm a perl bitch!

on July 2, 2003 11:05 PM
# Scott Johnson said:

$data = file_get_contents ('file.txt');

I'm a php bitch !

on July 3, 2003 03:42 AM
# Dave Winer said:

UserTalk:

s = file.readWholeFile ("c:\tmp.txt")

BTW, I read your comment on Russell Beattie's and just wanted to acknowledge it. Really hurt my feelings there Jeremy, of course I'm sure that's what you wanted to do, so congrats.

on July 3, 2003 05:27 AM
# Jeremy Zawodny said:

Eh?

Dave, I didn't think *you* started the whole Echo mess, did you?

on July 3, 2003 07:30 AM
# Bill Brown said:

ColdFusion:

on July 3, 2003 08:51 AM
# Bill Brown said:

ColdFusion:

<cffile action="read" file="test.txt" variable="foo">

(Sorry, forgot to escape out the tag!)

on July 3, 2003 08:51 AM
# Dave Winer said:

>>Dave, I didn't think *you* started the whole Echo mess, did you?

Nope, I sure didn't. If you go back and read the comments on Beattie's blog, you'll see that they were talking about me, and your agreement, clearly unintentional, was about hating me. Whatever.

on July 3, 2003 10:09 AM
# Mark said:

Every time I see someone complain about "that should be in the Java standard library" I can't help but thinking that person can't be bothered to write one simple little class.

Yes, the syntax for reading a file is overly verbose. Who cares? Does it really take more time to write a 3-line while block, versus a 1-line read statement?

Also, I don't see _any_ of the above examples able to gracefully handle any exceptions. This is why all of your code sucks, people. This is why people hate the web, because of all the errors.

What happens when the file isn't there? What happens when the file is garbled? What happens if the permissions on the file cause it to be unable to be read at that time?

Here's what happens:

Perl: program barfs and exits unless you write a _ton_ of supporting code (and "|| die" doesn't count).

PHP: page barfs with an unreadable error message.

Cold Fusion: page barfs unless you've written a try...catch loop (hey, that's Java!) -- props to Macromedia/Allaire, though: the barf message is a thrown Exception in MX! High comedy.

Java? Throws an exception which you can catch and deal with gracefully.

on July 3, 2003 12:06 PM
# Mark said:

...and Dave, don't you have anything better to do than go around reading about yourself all day, in order to see who likes you and who doesn't?

Maybe like... write a little more code? Especially of the error-trapping kind?

on July 3, 2003 12:09 PM
# netron said:

i'm a php bitch as well - cant stand Java. Tried it for a few years (mid 90s) and just gave up - IT expected ME to spend a LOT of TIME on it. No thanks - i dont have enough time.

php :

$my_file_array=file("/home/username/mytextfile");

$line_one_of_file=$my_file_array[0];
$line_two_of_file=$my_file_array[1];

More info:
http://www.php.net/file

on July 3, 2003 03:12 PM
# kasia said:

Mark,.. that's why I said it should take a File object.. after you build one of those the file better exist or you have some serious coding problem :)

Anyway.. if it's something that people have to build uitilities classes for on regular basis.. it should just be part of standard libraries.. yah, it is easy.. but So's making a String class.

on July 3, 2003 03:46 PM
# netron said:

all your Java are belong to us PHP heads.

although , i have my doubts with PHP5 beta.. major PR fcukup there.

on July 3, 2003 03:51 PM
# Kev Spencer said:

Mark,

>>Perl: program barfs and exits unless you write a _ton_ of supporting code...

Er, no, a Perl program won't automatically barf and exit when attempting to open a file unless you tell it to. Error handling can be as simple as this though:

open(FILE, $file) || do {
# error handling here
};

on July 3, 2003 04:40 PM
# Arien said:

Mark: In Perl die-ing is throwing an exception. Don't like having to type "die"? Use Fatal.

As for PHP, have a look at this page on errors in the manual.

on July 3, 2003 05:16 PM
# Sasa Velickovic said:

Nothing simpler than Ruby ;)

def File.read_all(fname)
  File.open(fname, 'r') {|f| return f.read }
end

def File.write(fname, str)
  File.open(fname, 'wb') {|f| f.write str }
end

on July 4, 2003 03:40 PM
# Anonymous said:

"Mark,.. that's why I said it should take a File object.. after you build one of those the file better exist or you have some serious coding problem :)"

No, File is also for file system entries that do not exist yet. It is used, among other things, to create new directories.

on July 5, 2003 01:12 PM
# kasia said:

sigh, everyone's an expert..

isFile() isDirectory().. shall I go on? Do your own error handling any way you want with it.

on July 5, 2003 07:31 PM
# Ehsan Rehman said:

Thats intressting

on September 7, 2003 04:45 AM
# Americanlifestyle said:

It's very instructive to learn something about Java Bashing

on September 20, 2003 07:16 AM
Disclaimer: The opinions expressed here are mine and mine alone. My current, past, or previous employers are not responsible for what I write here, the comments left by others, or the photos I may share. If you have questions, please contact me. Also, I am not a journalist or reporter. Don't "pitch" me.

 

Privacy: I do not share or publish the email addresses or IP addresses of anyone posting a comment here without consent. However, I do reserve the right to remove comments that are spammy, off-topic, or otherwise unsuitable based on my comment policy. In a few cases, I may leave spammy comments but remove any URLs they contain.