The File class in Java already creates txt files ???, Javatxt

Source: Internet
Author: User

The File class in Java already creates txt files ???, Javatxt

Private JButton getOpenButton (){
If (openButton = null ){
OpenButton = new JButton ();
OpenButton. setText ("Write File"); // prompt message for modifying the button
OpenButton. addActionListener (new java. awt. event. ActionListener (){
// Button click event
Public void actionreceivmed (ActionEvent e ){
// Create a file object
File file = new File ("word.txt ");
Try {
// Create a FileWriter object
FileWriter out = new FileWriter (file );
// Obtain the text in the text field
String s = jTextArea. getText ();
Out. write (s); // write information to the disk file.
Out. close (); // close the stream
} Catch (Exception e1 ){
E1.printStackTrace ();
}
}
});
}
Return openButton;
}

Private JButton getCloseButton (){
If (closeButton = null ){
CloseButton = new JButton ();
CloseButton. setText ("Read File"); // The prompt message for modifying the button
CloseButton. addActionListener (new java. awt. event. ActionListener (){
// Button click event
Public void actionreceivmed (ActionEvent e ){
File file = new File ("word.txt"); // create a File object
Try {
// Create a FileReader object
FileReader in = new FileReader (file );
Char byt [] = new char [1024]; // create a char array
Int len = in. read (byt); // read bytes into the Array
// Set the display information of text fields
JTextArea. setText (new String (byt, 0, len ));
In. close (); // close the stream
} Catch (Exception e1 ){
E1.printStackTrace ();
}
}
});
}
Return closeButton;

}

Starting from the beginning, I thought that the woed.txt file was re-created for both buttons. Isn't it overwritten?

If a file is not created on the hosts file, the word.txt file Created by the fileclass is not actually created. To create a file, use the file. creatNewfile (). In fact, both are new File ("word.txt"), but the cache is temporarily created on the disk.

And because the first key has been created, the second key will be used directly (with the same name ).

Related Article

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.