程序代码如下:
//<Applet code=AppletFileInputStream width=300 height=400></Applet>
import java.awt.*;
import java.io.*;
import java.applet.*;public class AppletFileInputStream extends Applet
{
byte buffer[]=new byte[5200];
int m_bytes;
TextArea hArea=new TextArea("",20,80);
public void init()
{
this.add(hArea);
}
public void paint(Graphics g)
{
try
{
FileInputStream file1=new FileInputStream("AppletFileInputStream.java");
m_bytes=file1.read(buffer,0,5200);
String hstr=new String(buffer,0,0,m_bytes);
hArea.appendText(hstr);
file1.close();
}
catch(Exception e)
{
hArea.appendText("read err"+"\n");
}
try
{
FileOutputStream file2=new FileOutputStream("temp.txt");
file2.write(buffer);
file2.close();
}
catch(Exception e)
{
hArea.appendText("write err"+"\n");
}
}
我的问题是书上说Applet不能读和写本地文件,但我在运行这个程序时(appletviewer AppletFileInputStream .java),文件内容可以读出来,显示,可是写文件就会抛出异常,为什么??书上不是说不能直接读吗?这儿能读,却写不了,不明白,望各位高人指教,谢谢。
//<Applet code=AppletFileInputStream width=300 height=400></Applet>
import java.awt.*;
import java.io.*;
import java.applet.*;public class AppletFileInputStream extends Applet
{
byte buffer[]=new byte[5200];
int m_bytes;
TextArea hArea=new TextArea("",20,80);
public void init()
{
this.add(hArea);
}
public void paint(Graphics g)
{
try
{
FileInputStream file1=new FileInputStream("AppletFileInputStream.java");
m_bytes=file1.read(buffer,0,5200);
String hstr=new String(buffer,0,0,m_bytes);
hArea.appendText(hstr);
file1.close();
}
catch(Exception e)
{
hArea.appendText("read err"+"\n");
}
try
{
FileOutputStream file2=new FileOutputStream("temp.txt");
file2.write(buffer);
file2.close();
}
catch(Exception e)
{
hArea.appendText("write err"+"\n");
}
}
我的问题是书上说Applet不能读和写本地文件,但我在运行这个程序时(appletviewer AppletFileInputStream .java),文件内容可以读出来,显示,可是写文件就会抛出异常,为什么??书上不是说不能直接读吗?这儿能读,却写不了,不明白,望各位高人指教,谢谢。
你在本地跑,和在IE上跑加载的安全管理器是不一样的,对应的权限也就不同。