About bufferedreader/writer and filedialog

Source: Internet
Author: User
Tags gettext
Package hxy.widget.com; import Java. AWT. *; import Java. AWT. event. *; import Java. io. *; import javax. swing. jframe; public class aboutdialog extends jframe implements actionlistener {/*****/Private Static final long serialversionuid = bytes; private filedialog filedialog_save, filedialog_load; private menubar; // menu bar private menu; // menu private menuitem itemopen, itemsave; // menu item private Textarea text; // text field private bufferedreader in; // read text from the character input stream, buffer each character, so as to achieve efficient reading of characters, arrays, and rows. Private filereader file_reader; // used to read the volume stream. To read the original byte stream, consider using fileinputstreamprivate bufferedwriter out; // write the text to the character output stream and buffer each character to provide efficient writing of a single character, array, and string. Private filewriter file_writer; // convenient class for writing character files public aboutdialog () {super ("Open/Save"); setsize (800,600); setvisible (true ); menubar = new menubar (); menu = new menu ("file"); itemopen = new menuitem ("Open File"); itemsave = new menuitem ("save file "); itemopen. addactionlistener (this); itemsave. addactionlistener (this); menu. add (itemopen); menu. add (itemsave); menubar. add (menu); setmenubar (menubar); filedialog_save = new filedialo G (this, "save file", filedialog. save); filedialog_load = new filedialog (this, "Open File", filedialog. load); filedialog_save.addwindowlistener (New windowadapter () {public void windowclosing (invalid wevent e) {filedialog_save.setvisible (false) ;}}); filedialog_load.addwindowlistener (New windowadapter () {public void windowclosing (invalid wevent e) {filedialog_load.setvisible (false) ;}}); addwindowlistener (New windowadapter () {Public void windowclosing (invalid wevent e) {system. Exit (0) ;}}); text = new textarea ("text field .. "); Add (text, borderlayout. center);} public void actionreceivmed (actionevent e) {If (E. getsource () = itemopen) {filedialog_load.setvisible (true); text. settext (null); // clear the text field string s; If (filedialog_load.getfile ()! = NULL) {try {file = new file (filedialog_load.getdirectory (), filedialog_load.getfile (); // obtain the opened file file_reader = new filereader (File ); in = new bufferedreader (file_reader); While (S = in. readline ())! = NULL) {// Readline () reads a text line text. append (S + '\ n'); // do not forget the line break} In. close (); // close the stream and release all associated resources. File_reader.close (); // close the stream and release all associated resources. Close () in filereader inherits from inputstreamreader} catch (ioexception E2) {}}} else if (E. getsource () = itemsave) {filedialog_save.setvisible (true); If (filedialog_save.getfile ()! = NULL) {system. out. print (filedialog_save.getfile (); try {file = new file (filedialog_save.getdirectory (), filedialog_save.getfile (); // obtain the file to be saved file_writer = new filewriter (File ); out = new bufferedwriter (file_writer);/** public void write (string S, * int off, * int Len) * throws ioexception * ----------------------- * s-the string to be written * Off-offset from the start to read the character * len-number of characters to be written **/out. write (text. gettext (), 0 ,( Text. gettext (). Length (); Out. Close (); // close the stream and release all associated resources. File_writer.close (); // close the stream and release all associated resources .} Catch (ioexception E2) {}}} public static void main (string ARGs []) {New aboutdialog ();}}

 

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.