Code for reading text files in the Java frame window

Source: Internet
Author: User

/*** Read the text file * javaalpha */import Java. io. *; import Java. AWT. *; import Java. AWT. event. *; public class jtxtfm {public static void main (string ARGs []) {jtxtfrm fm = new jtxtfrm (); // here is Program Where execution starts, create a new window} class jtxtfrm extends frame implements actionlistener {filedialogop, SV; // file dialog box buttonbtn1, btn2, btn3; // button textareatarea; // text box jtxtfrm () {super ("read/write files"); // Title setlayout (null); // layout setbackground (color. cyan); // background color setsize (600,300); // set setsetvisible (true); // set visible btn1 = new button ("open "); // create button btn2 = new button ("save"); btn3 = new button ("close"); Tarea = new textarea (""); // create a text box add (btn1); // Add button add (BTN 2); add (btn3); add (Tarea); Tarea. setbounds (30, 50,460,220); // set the position of the text box btn1.setbounds (520, 60, 50, 30); btn2.setbounds (520,120, 50, 30); btn3.setbounds (520,180, 50, 30); OP = new filedialog (this, "open", filedialog. load); // create a dialog box SV = new filedialog (this, "save", filedialog. save); btn1.addactionlistener (this); // click the listener event btn2.addactionlistener (this); btn3.addactionlistener (this); addwindowlistener (New windowada Pter () {// Close event processing public void windowclosing (javaswevent e) {setvisible (false); // set Invisible system. exit (0); // program closed});} public void actionreceivmed (actionevent e) {If (E. getsource () = btn1) {// button 1 event processing string STR; OP. setvisible (true); // set the visible try {file F1 = new file (op. getdirectory (), op. getFile (); // open the file filereader Fr = new filereader (F1); bufferedreader BR = new bufferedreader (FR); // read the file Tarea. settext (""); // set the text box Empty while (STR = Br. Readline ())! = NULL) Tarea. append (STR + '\ n'); // Add the Read File Content in the text box Fr. close (); // close read} catch (exception E1) {// if an error occurs, handle e1.printstacktrace () here; // print the error message} If (E. getsource () = btn2) {// button 1 event processing Sv. setvisible (true); try {file F1 = new file (Sv. getdirectory (), Sv. getFile (); filewriter fw = new filewriter (F1); bufferedwriter BW = new bufferedwriter (FW); string GT = Tarea. gettext (); // get the text box content BW. write (GT, 0, GT. length (); // write the text box content to the file BW. flush (); // The Real Write File FW. close (); // close write} catch (exception E2) {// error handling e2.printstacktrace (); // print error message} If (E. getsource () = btn3) {// button 3 event processing system. exit (0); // close the program }}}

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.