applet 读写客户端文件

在前面已经提及了如何的读客户端的文件,写也是和以上差不多,下面是一个创建文件,并写入的java applet的代码

import java.applet.*;
import java.io.*;

import java.awt.event.*;
import javax.swing.*;

public class wapp extends Applet {
public void init() {
JButton button = new JButton("Create a file");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
File file = new File("c://a.txt");
try {
file.createNewFile();
FileWriter filew=new FileWriter("c://a.txt");
String str = "asdfasd";
for ( int i = 0; i<str.length() ; i++ )
{
char c = str.charAt( i );
filew.write(c);
}
filew.close();
//filew.append( "/n" + "保存文件成功了,呵呵" );


JOptionPane.showMessageDialog(null, "成功创建文件c://a.txt",
"消息", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "错误",
JOptionPane.ERROR_MESSAGE);
}
}
});
add(button);
}

}

这里重要的是想说一下,用policytool创建权限:

在C:/Program Files/Java/jdk1.5.0_12/bin下找到policytool.exe

运行,添加项目,然后选择 添加权限,

1 allpermission

2 Filepermission;<<ALL FILES>>;read, write, delete, execute

3propertypermission;user.home;read;

保存,然后将生成的代码,复制到apple 的项目文件夹中的applet.policy

keystore "file:http://local...8080/../签名.store", "JKS";

grant {
permission java.security.AllPermission;
permission java.util.PropertyPermission "user.home", "read";
permission java.io.FilePermission "<<ALL FILES>>", "read, write, delete, execute";
};

运行,完毕

你可能感兴趣的:(applet)