[2193] in java-interest
Re: gets, fgets equivalents
daemon@ATHENA.MIT.EDU (Chuck McManis)
Tue Sep 26 18:04:52 1995
Date: Tue, 26 Sep 1995 12:06:07 -0700
From: cmcmanis@scndprsn.Eng.Sun.COM (Chuck McManis)
To: java-interest@java.Eng.Sun.COM, Elliotte@blackstar.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?
In alpha3 try java.io.TextInputStream.readLine();
in beta try java.io.DataInputStream.readLine();
Canonical example:
class CopyFile {
public static void main(String args[]) {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(args[0]);
out = new FileOutputStream(args[1]);
} catch (Throwable e) {
System.out.println("Usage: java CopyFile file1 file2");
System.exit(1);
}
// fgets equivalent
DataInputStream dis = new DataInputStream(in);
// fputs equivalent
PrintStream ps = new PrintStream(out);
String tmp;
while ((tmp = dis.readLine()) != null)
ps.println(tmp);
in.close();
out.close();
}
}
--Chuck
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com