package PKLesson6;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileNotFoundException;//import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.RandomAccessFile;//import java.awt.GridLayout;import javax.swing.JApplet;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JTextField;public class Drrp extends JApplet implements ActionListener ...{ JPanel pl = new JPanel(); JButton b1 = new JButton("保存"); JButton b2 = new JButton("读取"); JLabel l1 = new JLabel("Name"); JLabel l2 = new JLabel("Phone"); JTextField tf1 = new JTextField(10); JPasswordField pwd = new JPasswordField(10); // JTextField tf2=new JTextField(20); GridBagLayout gby = new GridBagLayout(); //GridLayout glt=new GridLayout(); GridBagConstraints gbc = new GridBagConstraints(); FileInputStream fis; BufferedReader br; InputStreamReader isr; public void init() ...{ this.getContentPane().add(pl); pl.setLayout(gby); //gbc.fill=GridBagConstraints.BOTH; //gbc=anchor.gridBagConstraints.CETER; gbc.gridx = 3; gbc.gridy = 1; gby.setConstraints(l1, gbc); pl.add(l1); gbc.gridx = 4; gbc.gridy = 1; gby.setConstraints(tf1, gbc); pl.add(tf1); gbc.gridx = 3; gbc.gridy = 2; gby.setConstraints(l2, gbc); pl.add(l2); gbc.gridx = 4; gbc.gridy = 2; gby.setConstraints(pwd, gbc); pl.add(pwd); gbc.gridx = 3; gbc.gridy = 3; gby.setConstraints(b1, gbc); pl.add(b1); gbc.gridx = 4; gbc.gridy = 3; gby.setConstraints(b2, gbc); pl.add(b2); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed(ActionEvent e) ...{ if (e.getSource() == b1) ...{ String n = tf1.getText(); char[] c = pwd.getPassword(); String p = String.copyValueOf(c); try ...{ RandomAccessFile raf = new RandomAccessFile("d:/pwd.txt", "rw"); raf.seek(raf.length()); //raf.length(); raf.writeBytes(n + " " + p); //raf.writeBytes(n); //raf.writeBytes( ); //raf.writeBytes(p); } catch (FileNotFoundException e1) ...{ e1.printStackTrace(); } catch (IOException e2) ...{ e2.printStackTrace(); } tf1.setText(""); pwd.setText(""); } else ...{ if (e.getSource() == b2) ...{ try ...{ fis = new FileInputStream("d:/pwd.txt"); isr = new InputStreamReader(fis); br = new BufferedReader(isr); String s = new String(); while ((s = br.readLine()) != null) System.out.println(s); //System.out.println(tf1); // System.out.println(pwd); fis.close(); } catch (FileNotFoundException fn) ...{ System.out.println("File not found!"); } catch (IOException fn) ...{ System.out.println("I/O Error"); } } } }}