如何从文件中读取数据并通过html页面中的applet显示数据 [英] How to read data from a file and display the same through applet in html page
问题描述
下面是我试图读取文件并想要在通过html页面打开的applet中显示其内容的Java源代码.问题是,当我尝试通过appletviewer打开小程序时,没有问题,数据可以成功显示,但同样不会发生,然后我通过浏览器打开小程序.
我没有通过浏览器打开相同文件的问题.
请帮忙...
以下是"fileapplet.java"文件的代码:
Below is the java source code where I am trying to read a file and want to display its content on an applet which is opened through a html page. The problem is that when I try to open the applet through appletviewer there is not problem the data is displayed successfully but the same does not happens then I open the applet through the browser.
I am not getting what is the problem in opening the same file through browser.
Please help...
Following is the code for "fileapplet.java" file:
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.util.Scanner;
public class fileapplet extends Applet
{
public void paint(Graphics g)
{
BufferedReader diskInput;
String word;
int a=0,b=0;
try
{
//reads in words from a file
diskInput = new BufferedReader(new InputStreamReader(new FileInputStream("Tree.txt")));
// file name is on command line
Scanner input=new Scanner(diskInput);
while (input.hasNext())
{
word=input.next();
word=word.toLowerCase(); // use lower case only
g.drawString(word,a,b);
a+=10;
b+=10;
}
}
catch (IOException e)
{
}
}
}
以下是"fileapplet.html"的代码:
Following is the code for "fileapplet.html":
<html>
<applet code="fileapplet.class" width="100" height="100">
</applet>
</html>
以下是"Tree.txt"文件的内容:
56 78 87 54
Following is the content of "Tree.txt" file:
56 78 87 54
推荐答案
您的小程序没有读取/写入I/O的权限.
您需要使用applet或更新.java.policy文件才能授予访问权限.
Your applet doesnt have permission to do read/write I/O.
You need to sing the applet or update your .java.policy file to give access permissions.
这篇关于如何从文件中读取数据并通过html页面中的applet显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!