import java.io.*;
import java.util.*;
import java.awt.*;
public class FileTestApplet extends java.applet.Applet implements Runnable
{
Record[] orec, irec;
int n_irec, n_orec;
private Thread runner = null;
boolean going = false;
public void start()
{
if (runner == null) { runner = new Thread(this); runner.start(); }
}
public void run()
{
try
{
irec = new Record[10];
n_irec = 0;
PrintStream os = new PrintStream(new FileOutputStream("filetest.dat"));
irec[1] = new Record("John Franco", "118 Fleming", "Cincinnati, Ohio");
irec[1].toStream(os);
n_irec++;
irec[2] = new Record("Peter Rabbit", "Chicago Zoo", "Chicago, Illinois");
irec[2].toStream(os);
n_irec++;
irec[3] = new Record("John Q. Student", "Sawyer Hall", "Firehouse, NY");
irec[3].toStream(os);
n_irec++;
irec[4] = new Record("New York Times", "Date and Time Hall", "NYC");
irec[4].toStream(os);
n_irec++;
going = true;
repaint();
}
catch(IOException e)
{
System.out.print("Error: " + e);
System.exit(1);
}
try
{
DataInputStream is = new DataInputStream(new FileInputStream("filetest.dat"));
try
{
orec = new Record[10];
n_orec = 0;
for (int i=1 ; i < 10 ; i++)
{
orec[i] = new Record("","","");
orec[i].splitLine(is.readLine());
n_orec++;
}
}
catch (java.lang.NullPointerException e) {}
}
catch(IOException e)
{
System.out.print("Error: " + e);
System.exit(1);
}
}
public void paint(Graphics g)
{
if (!going) return;
g.drawString("Records sent to filetest.dat:", 10, 10);
for (int i=1 ; i <= n_irec ; i++) irec[i].drawRecord(g, 20, 15+i*15);
g.drawString("Records received from filetest.dat:", 10, 100);
for (int i=1 ; i <= n_orec ; i++) orec[i].drawRecord(g, 20, 115+i*15);
}
}
class Record
{
private String name;
private String street;
private String city;
public Record(String n, String s, String c)
{ name = n; street = s; city = c; }
public void drawRecord(Graphics g, int x, int y)
{
g.drawString(name + " " + street + " " + city, x, y);
}
public void toStream(PrintStream os) throws IOException
{
os.print(name); os.print("|");
os.print(street); os.print("|");
os.print(city); os.print("|\r\n");
}
public void splitLine(String s) throws IOException
{
StringTokenizer t = new StringTokenizer(s, "|");
name = t.nextToken();
street = t.nextToken();
city = t.nextToken();
}
}