首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

有关java.security.AccessControlException的有关问题

2011-11-22 
有关java.security.AccessControlException的问题写了一个程序importjavax.swing.*importjava.awt.*impo

有关java.security.AccessControlException的问题
写了一个程序
import   javax.swing.*;
import   java.awt.*;
import   java.awt.event.*;
import   java.io.*;
public   class   FileGUIBak   extends   JApplet   implements   ActionListener
{
private   JLabel   prompt   =   new   JLabel( "prompt ");
private   JTextField   nameInput   =   new   JTextField(20);
private   JButton   read   =   new   JButton( "ReadFile ");
private   JButton   write   =   new   JButton( "WriteFile ");
private   JTextArea   display   =   new   JTextArea(10,40);
private   JPanel   control   =   new   JPanel();
private   JPanel   displayPanel   =   new   JPanel();
private   void   writeTextFile(JTextArea   dis,   String   fileName){
try{
FileWriter   outStream   =   new   FileWriter(fileName);
outStream.write(dis.getText());
outStream.close();
}
catch(IOException   e){
display.setText( "IOERROR:   "   +e.getMessage()   +   "\n ");
e.printStackTrace();
}
}
private   void   readTextFile(JTextArea   dis,   String   fileName){
try{
BufferedReader   inStream   =   new   BufferedReader(new   FileReader(fileName));
String   line   =   inStream.readLine();
while(line   !=   null){
dis.append(line   +   "\n ");
line   =   inStream.readLine();
}
inStream.close();
}
catch(FileNotFoundException   e){
display.setText( "IOERROR:   File   NOT   Found:   "   +   fileName   +   "\n ");
e.printStackTrace();
}
catch(IOException   e){
display.setText( "IOERROR:   "   +   e.getMessage()   +   "\n ");
e.printStackTrace();
}
}
private   void   initControl(){
read.addActionListener(this);
write.addActionListener(this);
control.setBorder(BorderFactory.createTitledBorder( "Control "));
control.setLayout(new   FlowLayout());
control.add(prompt);
control.add(nameInput);
control.add(read);
control.add(write);
}
private   void   initDisplay(){
displayPanel.setBorder(BorderFactory.createTitledBorder( "Display "));
displayPanel.setLayout(new   FlowLayout());
display.setLineWrap(true);
displayPanel.add(new   JScrollPane   (display));
}
public   void   init(){
initControl();
initDisplay();
getContentPane().setLayout(new   BorderLayout());
getContentPane().add(control, "North ");
getContentPane().add(displayPanel, "Center ");
getContentPane().setSize(500,300);
}
public   void   actionPerformed(ActionEvent   e){
if(e.getSource()   ==   read){
readTextFile(display,nameInput.getText());
}
else{
writeTextFile(display,nameInput.getText());
}
}
};
和FileGUIBak.htm
<html>
<body>
<table   align= "center ">
<td>
<applet   code= "FileGUIBak.class "   width= "500 "   height= "300 ">
</applet>
</td>
</table>


</body>
</html>
用appletview时,   read可用而write不可用报错如下  
java.security.AccessControlException:access   denied
可是一样的程序写成JFrame的就可以
import   javax.swing.*;
import   java.awt.*;
import   java.awt.event.*;
import   java.io.*;
public   class   TextIO   extends   JFrame   implements   ActionListener
{
private   JTextArea   display   =   new   JTextArea();
private   JButton   read   =   new   JButton( "Read   From   File ");
private   JButton   write   =   new   JButton( "Write   to   File ");
private   JTextField   nameField   =   new   JTextField(20);
private   JLabel   prompt   =   new   JLabel( "Filename   : ",   JLabel.RIGHT);
private   JPanel   commands   =   new   JPanel();
public   TextIO(){
super( "TextIO   Demo ");
read.addActionListener(this);
write.addActionListener(this);
commands.setLayout(new   GridLayout(2,2,1,1));
commands.add(prompt);
commands.add(nameField);
commands.add(read);
commands.add(write);
display.setLineWrap(true);
this.getContentPane().setLayout(new   BorderLayout());
this.getContentPane().add( "North ",   commands);
this.getContentPane().add(   new   JScrollPane(display));
this.getContentPane().add( "Center ",display);
}
private   void   readTextFile(JTextArea   display,   String   fileName){
try{
BufferedReader   inStream   =   new   BufferedReader(new   FileReader(fileName));
String   line   =   inStream.readLine();
while(line   !=   null){
display.append(line   +   "\n ");
line   =   inStream.readLine();
}
inStream.close();
}
catch(FileNotFoundException   e){
display.setText( "IOERROR:   FILE   NOT   FOUND:   "   +   fileName   +   "\n ");
e.printStackTrace();
}
catch(IOException   e){
display.setText( "IOERROR:   "   +   e.getMessage()   +   "\n ");
e.printStackTrace();
}
}
private   void   writeTextFile(JTextArea   display,String   fileName){
try{
FileWriter   outStream   =   new   FileWriter(fileName);
outStream.write(display.getText());
outStream.close();
}
catch(IOException   e){
display.setText( "IOERROR:   "   +e.getMessage()   +   "\n ");
e.printStackTrace();
}
}
public   void   actionPerformed(ActionEvent   e){
String   fileName   =   nameField.getText();
if(e.getSource()   ==   read){
display.setText( " ");
readTextFile(display,   fileName);
}
else
writeTextFile(display,   fileName);
}
public   static   void   main(String   args   [   ]){
TextIO   tio   =   new   TextIO();
tio.setSize(400,200);
tio.setVisible(true);
tio.addWindowListener(new   WindowAdapter(){
public   void   windowClosing(WindowEvent   e){
System.exit(0);
}
});
}
}
不知为什么,请高手指教

------解决方案--------------------


顶!

热点排行
Bad Request.