[2181] in java-interest

home help back first fref pref prev next nref lref last post

Re: gets, fgets equivalents

daemon@ATHENA.MIT.EDU (Thomas Ball)
Tue Sep 26 16:54:11 1995

Date: Tue, 26 Sep 1995 10:41:54 -0700
From: Thomas.Ball@Eng.Sun.COM (Thomas Ball)
To: Elliotte@blackstar.com
Cc: java-interest@java.Eng.Sun.COM

>  Does java have any equivalent to C's gets or fgets?  Or do I need to 
>  just read a series of bytes into an array and just convert it into a 
>  string? If the latter do the bytes in the standard input stream come 
>  in as ASCII or as Unicode?  Does anyone have sample code to do this?

The stream classes in java.io are a series of filters which can be
combined to get the functionality you desire.  The DataInputStream has
a readLine() method, so to emulate gets() use:

	DataInputStream in = new DataInputStream(System.in);
	String line = in.readLine();

To emulate fgets() try (as in, I didn't copy this code from a working
class, so it may be off):

	FileInputStream fis = new FileInputStream("my file name");
	BufferedInputStream bis = new BufferedInputStream(fis);
	DataInputStream in = new DataInputStream(bis);
	String line = in.readLine();

That last bit is normally written here as:

	DataInputStream in = new DataInputStream(
	    new BufferedInputStream(
	    new FileInputStream("my file name")));

Which better demonstrates the filtering, but may be too lisp-ish for some.

Tom
	
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post