Jay Kandy
Posts: 77
Nickname: jay
Registered: Mar, 2002
|
|
Re: applet to application
|
Posted: Jul 26, 2002 12:17 PM
|
|
Glad you learnt! Heres how you read a file like that. I posted the whole code coz i dint want to confuse you with just one method. After this, if you like, I want you to download javadocs if you dont have already, and read about ObjectInputStream and ObjectOutputStream and see how that can be used with this. Good Luck.
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.Graphics;
import java.awt.Color;
import java.applet.Applet;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Iterator;
import javax.swing.JApplet;
import javax.swing.JFrame;
public class ReadApplet extends JApplet
{
public static final String PROPERTIES_FILE = "app.prp";
private ArrayList list;
public ReadApplet(){}
public void explicitInit()
{
try
{
InputStream is = getClass().getResourceAsStream(PROPERTIES_FILE);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
doInit(br);
}
catch(Exception e)
{
handleException(e);
}
}
public void init()
{
try
{
URL filePath = new URL(getCodeBase() + PROPERTIES_FILE);
BufferedReader br = new BufferedReader(new InputStreamReader(filePath.openStream()));
doInit(br);
}
catch(Exception e)
{
handleException(e);
}
}
public void doInit(BufferedReader reader) throws Exception
{
list = new ArrayList();
String buffer;
String firstName = null;
String lastName = null;
int id = 0;
String email = null;
while( (buffer = reader.readLine()) != null )
{
StringTokenizer tokenizer = new StringTokenizer(buffer, "|");
while(tokenizer.hasMoreTokens())
{
firstName = tokenizer.nextToken();
lastName = tokenizer.nextToken();
id = Integer.parseInt(tokenizer.nextToken());
email = tokenizer.nextToken();
Subscriber aSubscriber = new Subscriber(firstName, lastName, id, email);
list.add(aSubscriber);
}
}
repaint();
}
public void paint(Graphics g)
{
Iterator iterator = list.iterator();
int i = 1;
while(iterator.hasNext())
{
Subscriber aSubscriber = (Subscriber)iterator.next();
g.drawString(aSubscriber.toString(), 100, 20 * i);
i++;
}
}
public void handleException(Exception e)
{
e.printStackTrace();
}
public static void main(String s[])
{
JFrame frame = new JFrame();
ReadApplet applet = new ReadApplet();
applet.explicitInit();
frame.getContentPane().add(applet);
frame.setSize(500, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class Subscriber
{
private String firstName;
private String lastName;
private int id;
private String email;
public Subscriber(String fn, String ln, int id, String e)
{
this.firstName = fn;
this.lastName = ln;
this.id = id;
this.email = e;
}
public String toString()
{
StringBuffer buffer = new StringBuffer("[ ");
buffer.append(firstName);
buffer.append(" " );
buffer.append(lastName);
buffer.append(", ID# ");
buffer.append(id);
buffer.append(", ");
buffer.append(email);
buffer.append(" ]");
return buffer.toString();
}
}
|
|