Example of swing file selector for Java learning notes (four file read/write methods-and image read/write methods)

Source: Internet
Author: User
Package capture. selector; import Java. AWT. image; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. AWT. image. bufferedimage; import Java. io. bufferedreader; import Java. io. bufferedwriter; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. filereader; import Java. io. filewriter; import Java. io. Ioexception; import Java. io. inputstreamreader; import Java. io. outputstreamwriter; import javax. swing. jbutton; import javax. swing. jfilechooser; import javax. swing. jframe; import javax. swing. jlabel; import javax. swing. joptionpane; import javax. swing. jscrollpane; import javax. swing. jtextarea; import javax. swing. swingutilities; import javax. swing. uimanager; import javax. swing. windowconstants; import Org. dyno. region L. swing. layouts. constraints; import Org. dyno. visual. swing. layouts. grouplayout; import Org. dyno. visual. swing. layouts. leading; import com.sun.image.codec.jpeg. export codec; import com.sun.image.codec.jpeg. export imageencoder; // vs4e -- Do not remove this line! /*** @ Author lushuaiyin * @ date 2011-10-18 */public class settest extends jframe {file targetfile; string message = ""; Private Static final long serialversionuid = 1l; private jtextarea jtextarea0; private jscrollpane jscrollpane0; private jbutton jbutton0; private jlabel jlabel1; private jbutton jbutton1; private jbutton jbutton2; Private Static final string preferred_look_and_feel = "javax. swing. plaf. meta L. metallookandfeel "; Public settest () {initcomponents ();} private void initcomponents () {setdefaclocloseoperation (windowconstants. dispose_on_close); setlayout (New grouplayout (); add (getjlabel1 (), new constraints (new leading (12, 12, 12), new leading (59, 22, 10, 10); add (getjbutton1 (), new constraints (new leading (52, 10, 10), new leading (142, 10, 10 ))); add (getjbutton0 (), new constraints (new leading (21 8, 10, 10), new leading (142, 12, 12); add (getjscrollpane0 (), new constraints (new leading (106,334, 10, 10 ), new leading (42, 51, 10, 10); add (getjbutton2 (), new constraints (new leading (329, 10, 10), new leading (142, 12, 12); setsize (501,240);} private jbutton getjbutton2 () {If (jbutton2 = NULL) {jbutton2 = new jbutton (); jbutton2.settext ("(image time) save as "); jbutton2.addactionlistener (New actionlistener () {Public void actionreceivmed (actionevent event) {try {jbutton2actionreceivmed (event);} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}});} return jbutton2;} private jbutton getjbutton1 () {If (jbutton1 = NULL) {jbutton1 = new jbutton (); jbutton1.settext ("select to read files"); jbutton1.addactionlistener (New actionlistener () {public void actionreceivmed (actionevent event) {JB Utton1actionactionreceivmed (event) ;}) ;}return jbutton1 ;}private jlabel getjlabel1 () {If (jlabel1 = NULL) {jlabel1 = new jlabel (); jlabel1.settext ("source file path");} return jlabel1;} private jbutton getjbutton0 () {If (jbutton0 = NULL) {jbutton0 = new jbutton (); jbutton0.settext ("Save as"); jbutton0.addactionlistener (New actionlistener () {public void actionreceivmed (actionevent event) {try {jbutton0actionpe Rformed (event);} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}});} return jbutton0;} private jscrollpane getjscrollpane0 () {If (jscrollpane0 = NULL) {jscrollpane0 = new jscrollpane (); response (getjtextarea0 ();} return jscrollpane0;} private jtextarea getjtextarea0 () {If (jtextarea0 = NULL) {jtextarea0 = new jtextarea (); Response ("jtextar Ea0 ");} return jtextarea0;} Private Static void installlnf () {try {string lnfclassname = preferred_look_and_feel; If (lnfclassname = NULL) lnfclassname = uimanager. getcrossplatformlookandfeelclassname (); uimanager. setlookandfeel (lnfclassname);} catch (exception e) {system. err. println ("cannot install" + preferred_look_and_feel + "on this platform:" + E. getmessage () ;}}/*** main entry of the class. * Note: This class is only created so that you can easily preview the result at runtime. * It is not expected to be managed by the designer. * You can modify it as you like. */public static void main (string [] ARGs) {installlnf (); swingutilities. invokelater (New runnable () {@ overridepublic void run () {settest frame = new settest (); frame. setdefaclocloseoperation (settest. exit_on_close); frame. settitle ("Se Ttest "); frame. getcontentpane (). setpreferredsize (frame. getsize (); frame. pack (); frame. setlocationrelativeto (null); frame. setvisible (true) ;}});} // read the file private void jbutton1actionactionreceivmed (actionevent event) {jfilechooser DLG = new jfilechooser ();/*** set the dialog box type, the method must be correct. Open the method control display page */DLG. setdialogtype (jfilechooser. open_dialog); // After setting the type and DLG. the Save dialog box is displayed only when the showopendialog (null) method is used together. // DLG. setdialogtype (J Filechooser. save_dialog); // After setting the type and DLG. the showdialog (null, "Haha") method is used together, and the first button is displayed as the second parameter "Haha ". // DLG. setdialogtype (jfilechooser. custom_dialog); // The parent component of the first parameter. The first parameter is the name of the first button (no longer open or saved) // DLG. showdialog (null, "Haha"); DLG. showopendialog (null); // DLG. showsavedialog (null); file = DLG. getselectedfile (); If (file = NULL) {This. message = "no file selected";} else {// What do you do after you get this file? system. out. println ("getabsolutepath --" + file. getabso Lutepath (); system. out. println ("getname --" + file. getname (); system. out. println ("isfile --" + file. isfile (); system. out. println ("isdirectory --" + file. isdirectory (); system. out. println ("getpath --" + file. getpath (); system. out. println ("getparent --" inclufile.getparent(%%%%this.tar GetFile = file; this. message = file. getpath (). tostring ();} jtextarea0.settext (this. message);} // save it as private void jbutton0actionactionreceivmed (Actionevent event) throws ioexception {fileinputstream FCM = NULL; inputstreamreader ISR = NULL; bufferedreader BR = NULL; fileoutputstream Fos = NULL; outputstreamwriter OSW = NULL; bufferedwriter BW = NULL; filereader Fr = NULL; filewriter fw = NULL; If (targetfile = NULL) {system. out. println ("the source file is empty. Select the file to save first! "); Joptionpane. showmessagedialog (null," the source file is empty. Select the file to save first! ");} Else {jfilechooser DLG = new jfilechooser (); DLG. setdialogtype (jfilechooser. save_dialog); DLG. showsavedialog (null); file getnewfile = DLG. getselectedfile (); string getnewpath = getnewfile. getpath (); string getnewname = getnewfile. getpath (); string getnewparent = getnewfile. getparent (); system. out. println ("getnewpath:" + getnewpath); system. out. println ("getnewname:" + getnewname); system. out. println ("getnewparent:: "+ Getnewparent); // write a new file. Try {// read the source file. The value is new fileinputstream(this.tar GetFile); ISR = new inputstreamreader (AIS); BR = new bufferedreader (ISR ); fr = new filereader(this.tar GetFile); // write the new file newfile = new file (getnewpath); If (! Newfile. exists () {newfile. createnewfile ();} Fos = new fileoutputstream (newfile); OSW = new outputstreamwriter (FOS); BW = new bufferedwriter (OSW); fw = new filewriter (newfile ); // The method for reading and writing files in Step 4 is provided below. All methods have passed the test. // Use bufferedreader and bufferedwriter to complete the experiment successfully. Note that a line break is added after reading a row! // String Ss = NULL; // while (Ss = Br. Readline ())! = NULL) {// BW. write (SS + "\ n"); // BW. flush (); // inputstreamreader and outputstreamwriter are used for the experiment. // char [] Buf = new char [2048]; // int num = 0; // while (num = ISR. read (BUF ))! = (-1) // {// whether to read the file // OSW. write (BUF, 0, num); // write the file data into the network buffer // OSW. flush (); // refresh the buffer to write data to the client //} // use filereader and filewriter for operation. The test is successful. // char [] cbuf = new char [1024]; // int nn = 0; // while (nn = Fr. read (cbuf ))! =-1) {// BW. write (cbuf, 0, NN); // BW. flush (); // use fileinputstream and fileoutputstream. The test is successful. byte [] B = new byte [512]; int N; while (n = FCM. read (B ))! =-1) {FOS. Write (n) ;}} catch (filenotfoundexception e) {e. printstacktrace () ;} finally {If (BW! = NULL) {BW. Close () ;}if (OSW! = NULL) {OSW. Close ();} If (FOS! = NULL) {FOS. Close () ;}if (BR! = NULL) {Br. Close () ;}if (ISR! = NULL) {ISR. Close () ;}if (fr! = NULL) {Fr. Close () ;}if (FW! = NULL) {FW. Close () ;}} joptionpane. showmessagedialog (null, "saved successfully! ") ;}} // Save the image as private void jbutton2actionreceivmed (actionevent event) throws ioexception {fileinputstream fis2 = NULL; inputstreamreader isr2 = NULL; bufferedreader Br2 = NULL; fileoutputstream fos2 = NULL; outputstreamwriter osw2 = NULL; bufferedwriter bw2 = NULL; filereader FR2 = NULL; filewriter fw2 = NULL; jfilechooser DLG = new jfilechooser (); DLG. setdialogtype (jfilechooser. save_dialog); DLG. showsavedialog (null ); File getnewfile = DLG. getselectedfile (); string getnewpath = getnewfile. getpath (); string getnewname = getnewfile. getpath (); string getnewparent = getnewfile. getparent (); system. out. println ("getnewpath:" + getnewpath); system. out. println ("getnewname:" + getnewname); system. out. println ("getnewparent:" + getnewparent); // write a new file try {// read the source file fis2 = new fileinputstream(this.tar GetFile); isr2 = new inputstreamreader (fis2); B R2 = new bufferedreader (isr2); image src = javax.imageio.imageio.read(this.tar GetFile); // construct the image object int Wideth = SRC. getwidth (null); // obtain the source image width, int Height = SRC. getheight (null); // get the source image length FR2 = new filereader(this.tar GetFile); // write the new file newfile = new file (getnewpath); If (! Newfile. exists () {newfile. createnewfile ();} fos2 = new fileoutputstream (newfile); osw2 = new outputstreamwriter (fos2); bw2 = new bufferedwriter (osw2); fw2 = new filewriter (newfile ); // graphics operation. The test is successful. bufferedimage tag = new bufferedimage (Wideth, height, bufferedimage. type_int_rgb); tag. getgraphics (). drawimage (SRC, Wideth, height, null); fos2 = new fileoutputstream (getnewpath); // output to the file stream using imageencoder encod ER = inclucodec. createjpegencoder (fos2); encoder. encode (TAG); // JPEG encoding} catch (filenotfoundexception e) {e. printstacktrace ();} finally {If (bw2! = NULL) {bw2.close ();} If (osw2! = NULL) {osw2.close ();} If (fos2! = NULL) {fos2.close ();} If (Br2! = NULL) {br2.close ();} If (isr2! = NULL) {isr2.close ();} If (FR2! = NULL) {fr2.close ();} If (fw2! = NULL) {fw2.close () ;}} joptionpane. showmessagedialog (null, "saved successfully! ");}}

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.