News for hackers. Stuff that matters.
Re:Why I like Java by cbsmith on Tuesday March 25, 2014 @09:55AM (#7465821)
Seriously? You've never read from or written to a stream?
Re:Why I like Java by lmm on Tuesday March 25, 2014 @12:34PM (#7466886)
I've never copied one stream to another by hand, byte by byte. There's a method in one of the apache commons libraries for that. But most of the time the only reason I'd ever read an input stream is using a CSV parser or a JSON parser or the like, in which case the idiom is usually to just hand the stream to the parser.
Re:Why I like Java by cbsmith on Tuesday March 25, 2014 @02:44PM (#7467914)
> I've never copied one stream to another by hand, byte by byte.

Yeah, but you've probably done a read and write independently. This is just about joining the two together.

> There's a method in one of the apache commons libraries for that.

Yup, and that's great to use that, and you'll no doubt end up with more efficient code most of the time if you do. But... you still should be able to write correct code for reading bytes from one stream, writing to another, without losing data in between.

Re:Why I like Java by lmm on Tuesday March 25, 2014 @03:26PM (#7468295)
> you still should be able to write correct code for reading bytes from one stream, writing to another, without losing data in between.

With access to Google and the library documentation sure, but I don't use those APIs often enough to be worth keeping in memory.

Re:Why I like Java by cbsmith on Tuesday March 25, 2014 @10:56PM (#7471265)
> With access to Google

Why the heck would you need Google

> and the library documentation

It's an online quiz.

> sure, but I don't use those APIs often enough to be worth keeping in memory.

You don't worry about it, but it tends to accumulate in one's mind anyway.

Here's the thing though. People are posting solutions on HN... and getting it wrong.

Re:Why I like Java by cs02rm0 on Thursday March 27, 2014 @04:28PM (#7483221)
Of course. These days though I don't tend to get much closer than something like Properties.load(InputStream). I really would have had to think about it.

I guess I've been stuck in webapps and NoSQL libraries for a while now.

Re:Why I like Java by smrtinsert on Tuesday March 25, 2014 @11:19AM (#7466384)
Where does HN find these guys... It's amazing how many long term "java" developers there are here.
Re:Why I like Java by cbsmith on Tuesday March 25, 2014 @02:45PM (#7467919)
I honestly am at a loss for this. Reading & writing data from a stream without losing any information seems like a pretty basic thing you'd want to know how to do in your chosen programming language.
Re:Why I like Java by peterashford on Tuesday March 25, 2014 @07:17PM (#7470035)
The reason for this is that if you're doing IO in Java you'll usually use some kind of buffered input reader rather than stdio. I've written a command line in Java that reads commands, runs native commands, streams output to the console and even that doesn't need to read stdio as a stream. It's not that Java can't do that, it's that there are better idioms for most problems.
Re:Why I like Java by cbsmith on Tuesday March 25, 2014 @11:01PM (#7471284)
> The reason for this is that if you're doing IO in Java you'll usually use some kind of buffered input reader rather than stdio.

The methods are the same with a buffered input reader.

> I've written a command line in Java that reads commands, runs native commands, streams output to the console and even that doesn't need to read stdio as a stream.

So, the thing that you are struggling with is how to get to System.in? But no problem doing System.out?

That's not even what the various code samples that have been posted have failed.

> It's not that Java can't do that, it's that there are better idioms for most problems.

Java's actually very good at doing IO efficiently, and as has been demonstrated, the code for this is quite simple. Sure, you wouldn't implement "cat" in Java, but you would read and write to sockets & files with it, which provides all the knowledge one might need to figure out how to solve this. As has been pointed out, the Apache Commons libraries make it so this is a two line program with no branching or looping. That's about as simple as it gets.

Re:Why I like Java by peterashford on Thursday March 27, 2014 @09:59PM (#7484838)
I'm not struggling with it at all. I'm an ex C,C++ programmer, i'd do a while not eof read loop.