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
open FILE, "file.txt";
@lines = <FILE>;
... and that's the *long* way to do it :) What can I say, I'm a perl bitch!
$data = file_get_contents ('file.txt');
I'm a php bitch !
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.
Eh?
Dave, I didn't think *you* started the whole Echo mess, did you?
ColdFusion:
<cffile action="read" file="test.txt" variable="foo">
(Sorry, forgot to escape out the tag!)
>>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.
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.
...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?
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
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.
all your Java are belong to us PHP heads.
although , i have my doubts with PHP5 beta.. major PR fcukup there.
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
};
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.
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
"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.
sigh, everyone's an expert..
isFile() isDirectory().. shall I go on? Do your own error handling any way you want with it.
It's very instructive to learn something about Java Bashing