当前位置: 技术问答>java相关
下面的源程序编绎通过,但不能浏览,是不是安全性的问题?
来源: 互联网 发布时间:2015-08-13
本文导语: 下面是完整的源程序,在JDK1.2下使用javac编译通过。 文本文件hqsj.txt的内容是: 600122宏图高科 18.90 18.80 18.90 18.20 18.27 3155 582.96 源程序: import java.io.*; import java.util.*; import java.awt.*; import java.applet.*; import ja...
下面是完整的源程序,在JDK1.2下使用javac编译通过。
文本文件hqsj.txt的内容是:
600122宏图高科 18.90 18.80 18.90 18.20 18.27 3155 582.96
源程序:
import java.io.*;
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class StockQuote extends Applet
implements ActionListener
{
private static final File INFO_FILE =
new File("hqsj.txt");
private Hashtable stockInfo;
TextField stockID;
Button button1;
private String quoteid,quotename;
public void init()
{
add(new Label("股票代码"));
stockID = new TextField(6);
add(stockID);
button1 = new Button("查询");
button1.addActionListener(this);
add(button1);
resize(500, 300);
}
public void start()
{
loadinfo();
}
protected boolean loadinfo()
{
String fileLine;
StringTokenizer tokenize;
String id;
StringBuffer name;
try {
// 创建一个访问数据文件的stream
BufferedReader stockInput = new
BufferedReader(new FileReader(INFO_FILE));
// 创建Hashtable对象
stockInfo = new Hashtable();
// 每次从文件中读一行数据
while ((fileLine = stockInput.readLine()) != null) {
// 将每一行数据分解为tokens.
tokenize = new StringTokenizer(fileLine);
try {
id = tokenize.nextToken();
// 创建一个放置股票信息的buffer
name = new StringBuffer();
while(tokenize.hasMoreTokens()) {
name.append(tokenize.nextToken());
if (tokenize.hasMoreTokens()) {
name.append("");
}
}
// 向Hashtable中充填记录
stockInfo.put(id,name.toString());
} catch(NullPointerException excpt) {
System.err.println("充填数据时出错: " + excpt);
} catch(NoSuchElementException excpt) {
System.err.println("无效的数据记录 " +
"in file: " + excpt);
}
}
stockInput.close();
} catch(FileNotFoundException excpt) {
System.err.println("不能发现文件: " + excpt);
return false;
} catch(IOException excpt) {
System.err.println("I/O故障: " + excpt);
return false;
}
return true;
}
protected String getQuote(String StockID)
{
String info;
// 从Hashtable得到数据
info = (String)stockInfo.get(StockID);
if (info != null)
return info;
else
return "股票代码错误!";
}
public void paint(Graphics g)
{
g.drawString("股票代码"+quoteid+":" ,10,60);
g.drawString("股票名称"+"前收"+"今开"+"最高"
+"最低"+"收盘"+"交易量"+"交易金额", 10, 90);
g.drawString(quotename, 10, 120);
}
public void actionPerformed(ActionEvent ev)
{
String label = ev.getActionCommand();
if (label.equals("查询"))
{
quoteid = stockID.getText();
if(quoteid != null)
quotename = getQuote(quoteid);
else quotename = "请输入股票代码!";
repaint();
}
}
}
但将class放在html中运行没有结果,是什么回事?
文本文件hqsj.txt的内容是:
600122宏图高科 18.90 18.80 18.90 18.20 18.27 3155 582.96
源程序:
import java.io.*;
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class StockQuote extends Applet
implements ActionListener
{
private static final File INFO_FILE =
new File("hqsj.txt");
private Hashtable stockInfo;
TextField stockID;
Button button1;
private String quoteid,quotename;
public void init()
{
add(new Label("股票代码"));
stockID = new TextField(6);
add(stockID);
button1 = new Button("查询");
button1.addActionListener(this);
add(button1);
resize(500, 300);
}
public void start()
{
loadinfo();
}
protected boolean loadinfo()
{
String fileLine;
StringTokenizer tokenize;
String id;
StringBuffer name;
try {
// 创建一个访问数据文件的stream
BufferedReader stockInput = new
BufferedReader(new FileReader(INFO_FILE));
// 创建Hashtable对象
stockInfo = new Hashtable();
// 每次从文件中读一行数据
while ((fileLine = stockInput.readLine()) != null) {
// 将每一行数据分解为tokens.
tokenize = new StringTokenizer(fileLine);
try {
id = tokenize.nextToken();
// 创建一个放置股票信息的buffer
name = new StringBuffer();
while(tokenize.hasMoreTokens()) {
name.append(tokenize.nextToken());
if (tokenize.hasMoreTokens()) {
name.append("");
}
}
// 向Hashtable中充填记录
stockInfo.put(id,name.toString());
} catch(NullPointerException excpt) {
System.err.println("充填数据时出错: " + excpt);
} catch(NoSuchElementException excpt) {
System.err.println("无效的数据记录 " +
"in file: " + excpt);
}
}
stockInput.close();
} catch(FileNotFoundException excpt) {
System.err.println("不能发现文件: " + excpt);
return false;
} catch(IOException excpt) {
System.err.println("I/O故障: " + excpt);
return false;
}
return true;
}
protected String getQuote(String StockID)
{
String info;
// 从Hashtable得到数据
info = (String)stockInfo.get(StockID);
if (info != null)
return info;
else
return "股票代码错误!";
}
public void paint(Graphics g)
{
g.drawString("股票代码"+quoteid+":" ,10,60);
g.drawString("股票名称"+"前收"+"今开"+"最高"
+"最低"+"收盘"+"交易量"+"交易金额", 10, 90);
g.drawString(quotename, 10, 120);
}
public void actionPerformed(ActionEvent ev)
{
String label = ev.getActionCommand();
if (label.equals("查询"))
{
quoteid = stockID.getText();
if(quoteid != null)
quotename = getQuote(quoteid);
else quotename = "请输入股票代码!";
repaint();
}
}
}
但将class放在html中运行没有结果,是什么回事?
|
在StringTokenizer类中系统缺省的分隔符是空格
|
安全性问题,APPLET不能读本地文件.