以下程序编译通过,本来是想把输入到文本框的内容写到文件中去,但是不行。如果先建一个空文件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);
}
}
}
}
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);
}
}
}
}
解决方案 »
- Java 编程求助 学兄学姐好 初学者特来求助 请指导的好心人帮助下
- 关于异常捕获异常写入文件
- 数据库题
- 给段代码,大家看看,是否合理
- Windows下Java如何使用数字证书
- HttpURLConnection请求图片,为何返回数据这么少?
- socket数据压缩传输问题
- 谁能解释一下java.util.Timer的“schedule”和“scheduleAtFixedRate”有什么区别?
- 请问哪位朋友有侯捷写的THINKING IN JAVA
- st = dbConn.createStatement();这一句总是报空指针异常的错误,求大神给看看
- 怎样取得jar包中的文件的路径
- 类似时间的计算
移到文件头试试,如果正确的话
就logFile.seek(logFile.length()-1);
如果用IE运行可能就不行了,Applet不能访问客户端资源
不能写入的原因是因为Applet不能访问客户端资源
,但是用appletviewer运行还是不可以存储
在网上搜索下,有很多的