[1599] in java-interest
String handling [ was RE: Java learning materials]
daemon@ATHENA.MIT.EDU (Chuck McManis)
Fri Sep 8 20:58:03 1995
Date: Fri, 8 Sep 1995 15:18:18 -0700
From: cmcmanis@scndprsn.Eng.Sun.COM (Chuck McManis)
To: nyu@gpu.srv.ualberta.ca, ajg@cadre.com
Cc: java-interest@java.Eng.Sun.COM
Someone wrote: (sorry I lost the original)
>> I find C++'s style natural(oxymoron?) and Java's reverts me back to the
>> low-level days of using C for string handling.
And then Andrew added:
>I use code similar to the following to provide a uniform interface to
>streaming objects.
[streaming interface]
[string as a subclass of String] (N.B. String is final and not subclassable)
usage of same as follows:
>class User implements Streamable {
> string name_;
> string id_;
> public void read( InputStream in ) {
> name_ = (new string())( in ); // not sure about the syntax here
> id_ = (new string())( in );
> ...
> }
In the alpha3 release there is a class java.io.TextInputStream that was designed
to read ASCII strings from files and return them as, well, Strings. This is analagous
to the old stdio version of fgets(). The two interesting properties of this class
are that it tried to handle end of line in a universal fashion, and it tried to
keep track of what line you were on. You would use it like this:
class User {
String name_;
String id_;
public void read (InputStream in) {
TextInputStream ti = new TextInputStream(in);
name_ = ti.readLine();
id_ = ti.readLine();
...
}
...
The idea is that subclasses of FilterInputStream could be used to add "intelligence"
to a stream for use by a Java class. Of course the opposite of TextInputStream
might have been called TextOutputStream but in fact it was PrintStream. The cool
thing about PrintStream is that it adds whatever the platform requires for line
terminators, so this simple program:
class foo {
public static void main(String args[]) {
TextInputStream ti = new TextInputStream(System.in);
String z = null;
while ((z = ti.readLine()) != null) System.out.println(z);
}
}
Would convert text pushed into standard in with arbitrary line ending conventions
to a file on standard out that had current platform line ending conventions. Trivial
I know but I seem to have written this program in C at least three times when going
from UNIX to DOS platforms.
--Chuck
>... {
> InputStream in = new FileInputStream( "/usr/..." );
> User user = (new User())( in );
>}
Of course in my example this remains the same.
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com