初学JAVA,在做一个IO流的练习用什么方法可以把输出的内容加在原txt文档内容的后面??
还有append和write有什么区别吗?我原来以为append是加在原文档内容后面,但是似乎和write一样关闭流后,就又重新开始,把原来的文档内容替换掉了望高手指教代码如下:import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Ex03 extends JDialog {
private static final long serialVersionUID = 20101003L;
private int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
private int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
private JLabel usernameLabel;
private JLabel passwordLabel;
private JTextField usernameText;
private JPasswordField passwordText;
private JButton submit;
private JButton reset;
private JPanel username;
private JPanel password;
private JPanel button; public Ex03() {
initialize();
} private void initialize() {
Container container = this.getContentPane();
this.setTitle("写入用户名密码");
this.setSize(200, 150);
this.setResizable(false);
this.setLocation((screenWidth - 200) / 2, (screenHeight - 150) / 2);
this.setLayout(new BorderLayout());
container.add(getUsername(), BorderLayout.NORTH);
container.add(getPassword(), BorderLayout.CENTER);
container.add(getButton(), BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
} private JLabel getUsernameLabel() {
if (usernameLabel == null) {
usernameLabel = new JLabel("用户名:");
}
return usernameLabel;
} private JLabel getPasswordLabel() {
if (passwordLabel == null) {
passwordLabel = new JLabel("密    码:");
}
return passwordLabel;
} private JTextField getUsernameText() {
if (usernameText == null) {
usernameText = new JTextField(10);
}
return usernameText;
} private JPasswordField getPasswordText() {
if (passwordText == null) {
passwordText = new JPasswordField(10);
}
return passwordText;
} private JButton getSubmit() {
if (submit == null) {
submit = new JButton("写入");
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg) {
File profile = new File("E:/00works/CODE/JAVA/[EX]/file",
"profile.txt");
try {
FileWriter writer = new FileWriter(profile);
BufferedWriter bufWriter = new BufferedWriter(writer);
String usernameContent = usernameText.getText();
bufWriter.append("用户名:" + usernameContent + "\r\n");
char passwordContent[] = passwordText.getPassword();
bufWriter.append("密  码:"
+ String.valueOf(passwordContent) + "\r\n\r\n");
bufWriter.close();
writer.close();
} catch (Exception err) {
err.printStackTrace();
}
}
});
}
return submit;
} private JButton getReset() {
if (reset == null) {
reset = new JButton("重置");
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg) {
usernameText.setText("");
passwordText.setText("");
}
});
}
return reset;
} private JPanel getUsername() {
if (username == null) {
username = new JPanel(new FlowLayout());
username.add(getUsernameLabel());
username.add(getUsernameText());
}
return username;
} private JPanel getPassword() {
if (password == null) {
password = new JPanel(new FlowLayout());
password.add(getPasswordLabel());
password.add(getPasswordText());
}
return password;
} private JPanel getButton() {
if (button == null) {
button = new JPanel(new FlowLayout());
button.add(getSubmit());
button.add(getReset());
}
return button;
} public static void main(String[] args) {
new Ex03();
}
}