Live data from Hacker News

Copying stdin to stdout in Java

gist.github.com

1–10 of 61 posts

Re: Copying stdin to stdout in Java

#6
post #5
post #3

Earlier quoted context omitted.

Yeah – what post?

"Why I like Java" by Mark Dominus: https://news.ycombinator.com/item?id=7463671

Who never wrote it but assumed we know that the same program in Perl, without using command line switches is

    while () {
        print;
    }
Still Awk is shorter:

     { print }
And if you are allowed change the switches of the command line to Perl for your program that Perl program can have exactly 0 bytes.

Re: Copying stdin to stdout in Java

#8
post #6
post #5

Earlier quoted context omitted.

"Why I like Java" by Mark Dominus: https://news.ycombinator.com/item?id=7463671

Who never wrote it but assumed we know that the same program in Perl, without using command line switches is while ( ) { print; } Still Awk is shorter: { print } And if you are allowed change the switches of the command line to Perl for your program that Perl program can have exactly 0 bytes .

Awk is even shorter: anything truthy works. You don't even need the print statement or brackets. Thus there are nine separate 1-byte programs which produce cat: [1], [2], [3], etc etc.

By default, unless you use the BEGIN block or something, awk will run your program on each line of stdlin. This is useful for programs of the type:

(/some regular expression/) { some action}

The default action if you don't specify one is "print $0" (the whole matching line). If your condition is a plain-ol' expression rather than a regular expression, and it always evaluates truthy, you thus get every line.

Re: Copying stdin to stdout in Java

#9
If you want a single point generalization about Java programmers, I was one for years, and the way I would have implemented this involves minimally a BufferedReader and an InputStreamReader. You'll also probably need to catch the IOException. Plus or minus 20 lines or so.

I don't hate Java, but "Java as it is spoken" does not often get used for Unix-style piping in a chain of small programs on a command line. It also isn't the easiest tool in the drawer if you want to do light (or heavy) string processing.

Re: Copying stdin to stdout in Java

#10
post #6
post #5

Earlier quoted context omitted.

"Why I like Java" by Mark Dominus: https://news.ycombinator.com/item?id=7463671

Who never wrote it but assumed we know that the same program in Perl, without using command line switches is while ( ) { print; } Still Awk is shorter: { print } And if you are allowed change the switches of the command line to Perl for your program that Perl program can have exactly 0 bytes .

Sure, but if you want to modify this code to send stdin over the network or through bluetooth in the Java version, you would just replace 'System.out' with the socket. The code would remain the same - in fact the whole class could be reused by having it pass in an input and output stream as parameters.

Your example is very much just a special shortcut for one special condition. It's not generalizable in the same way. And let's be honest - most of today's code is not writing to standard output, it is writing to some iOS or Android GUI or outputting JSON to be used in some javascript webapp. That kind of shortcut just doesn't make sense anymore and is a relic of a different time.

Post reply on HN