如何在java中使用小程序编写文本文件?

How to write text file using applet in java?

我正在尝试创建一个响应鼠标单击的小程序,然后在屏幕上绘制正方形并将鼠标坐标存储在 string.Finally 中,如果我按 'S' 键然后它保存名为 "Level1.txt" 的文件中的字符串。 但是,当我在 运行 这个小程序并按 S 时,file.I 中什么也没有保存,reason.Please 没有帮助。 这是我的代码:-

 import java.io.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
public class levelEditor extends Applet{
int x,y;
boolean clicked;
FileWriter fw;
PrintWriter pw;
BufferedWriter bw;
String pix;
    public void init() {
        setSize(500,500);
        x=0;
        y=0;
        pix="";

        clicked=false;
        addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e){
                clicked=true;
                x=e.getX();
                y=e.getY();
                pix=pix+x+","+y+" ";
                repaint();
            }
        } );
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e){
                try{
                    if(e.getKeyCode()==KeyEvent.VK_S){
                        fw=new FileWriter("Level1.txt");
                        bw=new BufferedWriter(fw);
                        pw=new PrintWriter(bw);
                        pw.println(pix);
                        pw.close();
                        System.exit(0);
                    }
                    }
                    catch(Exception exp)
                    {

                    }
            }
        });

    }
    public void paint(Graphics g){
        update(g);
    }
public void update(Graphics g){
    if(clicked){
    g.setColor(Color.GREEN);
    g.fillRect(x-5, y-5,10,10);

    }
}
}

您需要一个已签名的小程序才能访问本地文件系统。请参阅 Oracle 文档:What Applets Can and Cannot do.