Java program file read and save instance Code

Source: Internet
Author: User

1 classRadiohereextendsJFrameImplementsActionListener2 {3     PrivateJTextArea Ta=NewJTextArea (10,20);4     PrivateJFileChooser jfc=NewJFileChooser (NewFile ("."));5     PrivateJButton Bopen,bsave;6      PublicRadiohere ()7     {8         Super("Document selection box Application");9JScrollPane ps=NewJScrollPane (TA);Tenbopen=NewJButton ("select"); OneBsave=NewJButton ("Disk");  ASetLayout (NewFlowLayout (flowlayout.center,15,10)); - Add (PS); - Add (bopen); the Add (bsave); -Bopen.addactionlistener ( This); -Bsave.addactionlistener ( This); -SetVisible (true); +SetSize (600,560); -     } +      Public Static voidMain (string[] args) A     { atRadiohere frm=NewRadiohere (); - frm.setdefaultcloseoperation (jframe.exit_on_close); -     } -      Public voidactionperformed (ActionEvent e) -     { -JButton jbt=(JButton) E.getsource (); in         if(jbt==bopen) -         { to             intStatus=jfc.showopendialog ( This); +             if(status!=jfilechooser.approve_option) -Ta.settext ("No File selected"); the             Else { *                 Try{ $File file1=jfc.getselectedfile ();Panax NotoginsengScanner scan=NewScanner (file1); -String info= ""; the                      while(Scan.hasnext ()) +                     { AString str=scan.nextline (); theinfo+=str+ "\ r \ n"; +                     } - Ta.settext (info); $                 } $                 Catch(FileNotFoundException ex) { -              -                 } the                 } -             }Wuyi             Else{ the                 intRe=jfc.showsavedialog ( This); -                 if(re==jfilechooser.approve_option) Wu                 { -                     Try{ AboutFile file2=jfc.getselectedfile (); $FileOutputStream f=NewFileOutputStream (file2); -Bufferedoutputstream out=NewBufferedoutputstream (f); -                         byte[] b=(Ta.gettext ()). GetBytes (); -Out.write (b,0, b.length); A out.close (); +                     } the                     Catch(IOException IE) -                     { $                     } the                 } the             } the     } the}

Can read txt and CPP documents, and can save the contents of textarea

Java program file read and save instance Code

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.