static PrintStream out; static PrintStream err; // goes to console
System.out.println("to stdout");
System.out.print("to stdout");
System.err.println("to stderr");
System.err.print("to stderr");
static InputStream in; // a BufferedInputStream
import java.applet.*;
import java.io.*;
public class StdinTest
extends Applet
{
public void init()
{
int c, i = 0;
try
while ( (c = System.in.read()) != -1 )
i++;
catch (IOException e) {}
System.out.println("Read " + i + " chars");
}
}
import java.applet.*;
import java.io.*;
public class FileObjectTest
{
static void showFileStatus(String name)
{
File f = new File(name);
if ( !f.exists() )
{
System.err.println("Can't find " + name);
return;
}
System.out.println("File " + name + " is " +
f.length() + " bytes long");
if ( f.isDirectory() )
System.out.println(" is a directory");
}
public static void main(String[] args)
{
for (int i=0; i<args.length; i++)
showFileStatus(args[0]);
}
}
import java.applet.*;
import java.awt.*;
import java.io.*;
public class DirObjectTest {
static DirectoryList dl;
public static void main(String[] args)
{
String name = args[0];
File f = new File(name);
if ( !f.exists() )
{
System.err.println("can't find " + name);
return;
}
String[] dirListing = f.list();
dl = new DirectoryList(name, dirListing);
}
}
class DirectoryList extends Frame {
public DirectoryList(String name, String[] files)
{
super("DirectoryList Window");
setLayout(new FlowLayout());
resize(300,300);
List list = new List(10, false);
for (int i=0; i<files.length; i++)
list.addItem(files[i]);
add(list);
setVisible(true);
}
}
import java.awt.*;
import java.io.*;
// write bytes 'abchello' to a file called "junk"
public class FileOutputStreamTest {
public static void main(String[] args)
throws IOException
{
byte[] someBytes = { 'a', 'b', 'c' };
String hello = "hello";
FileOutputStream f = new FileOutputStream("junk");
if ( f != null )
{
f.write(someBytes);
for (int i=0; i<hello.length(); i++)
f.write((int)hello.charAt(i));
f.close();
}
}
}
import java.awt.*;
import java.io.*;
// write 34 binary then 'hello' as bytes to "junk"
public class DataOutputStreamTest {
public static void main(String[] args)
throws IOException
{
int i = 34;
String hello = "hello";
FileOutputStream f =
new FileOutputStream("junk");
DataOutputStream df = new DataOutputStream(f);
df.writeInt(i);
df.writeBytes(hello);
df.close();
f.close(); // close in reverse order
}
}
(in image form)
import java.awt.*;
import java.io.*;
public class PrintStreamTest {
public static void main(String[] args)
throws IOException
{
Button anArbitraryObject = new Button("Press");
FileOutputStream f =
new FileOutputStream("junk");
PrintStream pf = new PrintStream(f);
pf.println(34);
pf.println("an int is " + 34);
pf.println(3.14159);
pf.println("This is a string");
// calls anArbitraryObject.toString()
pf.println(anArbitraryObject);
pf.close();
f.close();
}
}
// make a generic output byte stream
f = new FileOutputStream("junk");
// attach BufferedOutputStream to buffer it
bf = new BufferedOutputStream(f);
bf.write(34);
import java.awt.*;
import java.io.*;
public class FileInputStreamTest {
public static void main(String[] args)
throws IOException
{
byte[] buffer = new byte[1024];
int b;
FileInputStream f = new FileInputStream("junk");
b = f.read(); // read a single byte
int bytes_read = f.read(buffer);
f.close();
}
}
import java.awt.*;
import java.io.*;
// reads data written by DataOutputStreamTest
// an int followed by a string.
public class DataInputStreamTest {
public static void main(String[] args)
throws IOException
{
int i;
FileInputStream f
= new FileInputStream("junk");
DataInputStream df = new DataInputStream(f);
int i = df.readInt();
String str = df.readLine();
System.out.println("Read " + i + " " + str);
df.close();
f.close();
}
}
import java.awt.*;
import java.io.*;
public class StringBufferInputStreamTest {
public static void main(String[] args)
throws IOException
{
String junk = "abc34def";
byte[] buffer = new byte[1024];
StringBufferInputStream f = new StringBufferInputStream(junk);
int c = f.read(); // reads 'a' into c
int n_read = f.read(buffer);
// buffer is {'b','c','3','4','d','e','f'}
f.close();
}
}
// open file junk for reading and writing
RandomAccessFile f = new RandomAccessFile("junk", "rw");
// open crud for reading only
File fo = new File("crud");
RandomAccessFile f = new RandomAccessFile(fo, "r");
f.seek(f.length());
f.seek(0);
import java.awt.*;
import java.io.*;
public class RandomAccessTest
{
public static void main(String[] args)
throws IOException
{
// get random access to file "junk"
RandomAccessFile f
= new RandomAccessFile("junk", "rw");
if ( f != null )
{
// write 34, 3.14159, "Hello" in binary
f.writeInt(34);
f.writeDouble(3.14159);
f.writeBytes("Hello");
f.seek(0); // rewind the file
int i = f.readInt();
double g = f.readDouble();
String str = f.readLine();
if ( i != 34 || g != 3.14159
|| !str.equals("Hello") )
System.out.println("Error");
f.close();
}
}
}
StreamTokenizer(InputStream) StreamTokenizer(Reader)
TT_EOL -- end of line token TT_EOF -- end of file token TT_NUMBER (nval) -- a number TT_WORD (sval) -- a word
int ttype; -- holds the type of token matched by nextToken()
eolIsSignificant(boolean) whiteSpaceChars(int low, int high) wordChars(int low, int high) nextToken() -- returns TT_EOF when done