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.visual.swing.layouts.Constraints;import org.dyno.visual.swing.layouts.GroupLayout;import org.dyno.visual.swing.layouts.Leading;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;//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.metal.MetalLookAndFeel";public Settest() {initComponents();}private void initComponents() {setDefaultCloseOperation(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(218, 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("(圖片時)另存新檔");jButton2.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) {try {jButton2ActionActionPerformed(event);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});}return jButton2;}private JButton getJButton1() {if (jButton1 == null) {jButton1 = new JButton();jButton1.setText("選擇讀取檔案");jButton1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) {jButton1ActionActionPerformed(event);}});}return jButton1;}private JLabel getJLabel1() {if (jLabel1 == null) {jLabel1 = new JLabel();jLabel1.setText("源檔案路徑");}return jLabel1;}private JButton getJButton0() {if (jButton0 == null) {jButton0 = new JButton();jButton0.setText("另存新檔");jButton0.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) {try {jButton0ActionActionPerformed(event);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});}return jButton0;}private JScrollPane getJScrollPane0() {if (jScrollPane0 == null) {jScrollPane0 = new JScrollPane();jScrollPane0.setViewportView(getJTextArea0());}return jScrollPane0;}private JTextArea getJTextArea0() {if (jTextArea0 == null) {jTextArea0 = new JTextArea();jTextArea0.setText("jTextArea0");}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.setDefaultCloseOperation(Settest.EXIT_ON_CLOSE);frame.setTitle("Settest");frame.getContentPane().setPreferredSize(frame.getSize());frame.pack();frame.setLocationRelativeTo(null);frame.setVisible(true);}});}//讀取檔案private void jButton1ActionActionPerformed(ActionEvent event) {JFileChooser dlg = new JFileChooser(); /** * 設定對話方塊類型,其開啟方法也要對應正確,開啟方法控制顯示介面 */dlg.setDialogType(JFileChooser.OPEN_DIALOG);//設定好類型後和dlg.showOpenDialog(null)方法配套使用才會顯示儲存對話方塊//dlg.setDialogType(JFileChooser.SAVE_DIALOG);//設定好類型後和dlg.showDialog(null, "haha")方法配套使用,並且第一個按鈕顯示為第二個參數"haha".//dlg.setDialogType(JFileChooser.CUSTOM_DIALOG);//第一個參數父組件,第2 個參數是第一個按鈕的名字(不再是開啟或儲存了)//dlg.showDialog(null, "haha");dlg.showOpenDialog(null);//dlg.showSaveDialog(null);File file=dlg.getSelectedFile();if(file==null){this.message="沒有選取檔案";}else{//得到這個檔案後,想幹嘛幹嘛System.out.println("getAbsolutePath--"+file.getAbsolutePath());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--"+file.getParent());this.targetFile=file;this.message=file.getPath().toString();}jTextArea0.setText(this.message);}//另存新檔private void jButton0ActionActionPerformed(ActionEvent event) throws IOException {FileInputStream fis=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("源檔案為空白,請先選擇要儲存的檔案!");JOptionPane.showMessageDialog(null, "源檔案為空白,請先選擇要儲存的檔案!");}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);//寫入新檔案try {//讀源檔案fis = new FileInputStream(this.targetFile);isr = new InputStreamReader(fis); br = new BufferedReader(isr); fr=new FileReader(this.targetFile); //寫新檔案 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); //下面提供了4中讀寫檔案方法,都已通過測試。//用BufferedReader,BufferedWriter 實驗成功,注意讀一行後加分行符號!//String ss=null;//while((ss=br.readLine())!=null){//bw.write(ss+"\n");//bw.flush();//}//用InputStreamReader,OutputStreamWriter 實驗成功//char[] buf=new char[2048]; //int num=0; //while((num=isr.read(buf))!=(-1)) //{ //是否讀完檔案 //osw.write(buf, 0, num);//把檔案資料寫出網路緩衝區 //osw.flush();//重新整理緩衝區把資料寫往用戶端 //} //用FileReader,FileWriter操作,實驗成功//char[] cbuf=new char[1024];//int nn=0;//while((nn=fr.read(cbuf))!=-1){//bw.write(cbuf,0,nn);//bw.flush();// }//用FileInputStream,FileOutputStream操作,實驗成功byte[] b = new byte[512];int n;while((n = fis.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, "儲存成功!");}}//圖片時另存新檔private void jButton2ActionActionPerformed(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);//寫入新檔案try {//讀源檔案fis2 = new FileInputStream(this.targetFile);isr2 = new InputStreamReader(fis2); br2 = new BufferedReader(isr2); Image src = javax.imageio.ImageIO.read(this.targetFile); //構造Image對象 int wideth=src.getWidth(null); //得到源圖寬 int height=src.getHeight(null); //得到源圖長 fr2=new FileReader(this.targetFile); //寫新檔案 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操作,實驗成功BufferedImage tag = new BufferedImage(wideth,height,BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src,0,0,wideth,height,null); fos2=new FileOutputStream(getNewPath); //輸出到檔案流 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos2); encoder.encode(tag); //JPEG編碼} 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, "儲存成功!");}}