473,324 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Java equivalent of stringstreams?

I am a longtime C++ programmer and I'm trying to understand how I would
phrase a common idiom in Java. Consider the following program in C++,
which reads tokens either from an istringstream (in memory) or from cin
(the console input):

#include <string>
#include <sstream>
#include <iostream>

using namespace std;

void readTokensFromStream(istream &in) {

string s;

while(in >> s) {
cout << "read from token: '" << s << "'" << endl;
}
}

int main() {

istringstream fromMem("a man a plan a canal panama");

readTokensFromStream(fromMem);

readTokensFromStream(cin);

return 0;
}

I'd like to be able to write something similar in Java, but it doesn't
appear so easy there. Whereas in C++, both istringstream and cin are
istreams, in Java you have StringReader (a reader, not a stream) and
System.in (an InputStream, not a reader). Is there a way to find a
common interface or adapter between the stream and reader/writer worlds
so that the program above can be elegantly stated in Java? My
definition of elegant would involve abstracting out the difference so
that readTokensFromStream could continue to be a single non-overloaded
method.

Thanks in advance.
Jordan

Nov 5 '05 #1
2 7943
jo****@madsam.com wrote:
I am a longtime C++ programmer and I'm trying to understand how I would
phrase a common idiom in Java. Consider the following program in C++,
which reads tokens either from an istringstream (in memory) or from cin
(the console input):

#include <string>
#include <sstream>
#include <iostream>

using namespace std;

void readTokensFromStream(istream &in) {

string s;

while(in >> s) {
cout << "read from token: '" << s << "'" << endl;
}
}

int main() {

istringstream fromMem("a man a plan a canal panama");

readTokensFromStream(fromMem);

readTokensFromStream(cin);

return 0;
}

I'd like to be able to write something similar in Java, but it doesn't
appear so easy there. Whereas in C++, both istringstream and cin are
istreams, in Java you have StringReader (a reader, not a stream) and
System.in (an InputStream, not a reader). Is there a way to find a
common interface or adapter between the stream and reader/writer worlds
so that the program above can be elegantly stated in Java? My
definition of elegant would involve abstracting out the difference so
that readTokensFromStream could continue to be a single non-overloaded
method.

Thanks in advance.
Jordan


An InputStream is for reading bytes, a Reader is for reading characters.
A Reader must know about character set and encoding issues whereas an
InputStream doesn't need to worry.

You can use InputStreamReader to convert an InputStream to a Reader (and
you must explicitly or implicitly supply a CharSet when you do this).

Since your readTokensFromStream function is concerned with reading
character information it should take a Reader as a parameter.

john
Nov 5 '05 #2
On 5 Nov 2005 06:49:19 -0800, jo****@madsam.com wrote, quoted or
indirectly quoted someone who said :
Is there a way to find a
common interface or adapter between the stream and reader/writer worlds
so that the program above can be elegantly stated in Java?


You use InputStreams for raw byte streams and Readers for streams of
encoded characters ( 8 or 16 bit ).

You can convert any InputStream to a Reader.

For sample code using the console as a Reader, see
http://mindprod.com/applets/fileio.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Nov 5 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
40
by: Eitan | last post by:
Hello, I know Java-Applet (written for JBuilder). I would like to know about dotnet technology, pros and cons in comparation to Java-Applet technololgy. Can I write a program in dotnet, like...
4
by: Jim H | last post by:
Does .NET have an equivalent to java's JMS messaging. I'm not a java programmer but as I understand it a java application can be setup to pass entire objects between processes. Is there some sort...
7
by: Mario | last post by:
I've been reading some Sun Java articles where they say Applets cannot import dlls and cannot write to disk. (This does NOT apply to applications, only to Applets.) Supposedly, this is NOT...
2
by: yaron | last post by:
Hi, for porting java to c# i need to know: What is the C#.NET equivalent for java.net.SocketAddress and java.nio.ByteBuffer ? Thanks.
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
26
by: Christoph Zwerschke | last post by:
You will often hear that for reasons of fault minimization, you should use a programming language with strict typing: http://turing.une.edu.au/~comp284/Lectures/Lecture_18/lecture/node1.html I...
6
by: mearvk | last post by:
Does C++ or C have something roughly equivalent to this: http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html What I need is a way to quickly convert between decimal and binary and...
5
by: Daniel | last post by:
Hi all, I have some questions regarding C#. I come from a Java background. I am using .NET 1.1. Please limit your answers to v 1.1, otherwise please state the version to which it applies to. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.