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