Private JButton Getopenbutton () {
if (Openbutton = = null) {
Openbutton = new JButton ();
Openbutton.settext ("Write file"); Modify the button's prompt information
Openbutton. addActionListener (New Java.awt.event.ActionListener () {
Button's Click event
public void actionperformed (ActionEvent e) {
To create a file object
File File = new file ("Word.txt");
try {
Create a FileWriter object
FileWriter out = new FileWriter (file);
Get text in a text field
String s = jtextarea.gettext ();
Out.write (s); Writing information to a disk file
Out.close (); Turn off the stream
} catch (Exception E1) {
E1.printstacktrace ();
}
}
});
}
return Openbutton;
}
Private JButton Getclosebutton () {
if (CloseButton = = null) {
CloseButton = new JButton ();
Closebutton.settext ("read file"); Modify the button's prompt information
CloseButton. addActionListener (New Java.awt.event.ActionListener () {
Button's Click event
public void actionperformed (ActionEvent e) {
File File = new filename ("Word.txt");//Create a Document object
try {
Create a FileReader object
FileReader in = new FileReader (file);
Char byt[] = new char[1024]; Creating an array of type Char
int len = In.read (byt); To read bytes into an array
Set the display information for a text field
Jtextarea.settext (New String (byt, 0, Len));
In.close (); Close the stream
} catch (Exception E1) {
E1.printstacktrace ();
}
}
});
}
return CloseButton;
}
As in the program section, I initially thought that the two keys have re-created the Woed.txt file, then not overwrite it?
In fact, the file class created word.txt files is not really created, really want to create, to use File.creatnewfile (), actually two places are new file ("Word.txt"), just temporarily create a cache on disk .
And because the first button has been created, the second one is used directly (the same name).
About Java inside the file class to create a txt file duplicate???