以下程序编译通过,本来是想把输入到文本框的内容写到文件中去,但是不行。如果先建一个空文件login,txt,也不行,不知道错在哪里。望指教?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;public class Login extends JApplet implements ActionListener
{
JTextField txtCustName;
JPasswordField txtPwd;

JLabel lblName;
JLabel lblPwd;
JButton btnLogin;

public void init()
{
createApplet();
}

public void createApplet()
{
Container content;
content = getContentPane();
content.setLayout(new FlowLayout());

lblName=new JLabel("Customer Name: ");
txtCustName=new JTextField(10);
lblPwd=new JLabel("Password: ");
txtPwd=new JPasswordField(10);
btnLogin=new JButton("Login");

content.add(lblName);
content.add(txtCustName);
content.add(lblPwd);
content.add(txtPwd);
content.add(btnLogin);

btnLogin.addActionListener(this);
}

public void actionPerformed(ActionEvent evt)
{
Object obj=evt.getSource();
if(obj==btnLogin)
{
String entry = txtCustName.getText() + ":" + new String(txtPwd.getPassword()) + "\n";
try
{
RandomAccessFile logFile = new RandomAccessFile("login.txt", "rw");
logFile.seek(logFile.length());
logFile.writeBytes(entry);
}
catch(IOException e)
{
this.showStatus("Cann't write to the file"+e);
}
}

}
}

解决方案 »

  1.   

    logFile.seek(logFile.length());
    移到文件头试试,如果正确的话
    就logFile.seek(logFile.length()-1);
      

  2.   

    用appletviewer运行可以存储成功啊。
    如果用IE运行可能就不行了,Applet不能访问客户端资源
      

  3.   

    TO xtaotao(淘淘) 
    不能写入的原因是因为Applet不能访问客户端资源
    ,但是用appletviewer运行还是不可以存储
      

  4.   

    Applet可以访问客户端资源,使用数字签名就可以
    在网上搜索下,有很多的