About Java inside the file class to create a txt file duplicate???

Source: Internet
Author: User

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???

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.