1. 程式人生 > >單檔案複製到指定目錄和自動執行多個.bat檔案實現(2012.06.05)

單檔案複製到指定目錄和自動執行多個.bat檔案實現(2012.06.05)

實現程式碼:

package com.cn.text;

/**
 *
 * @author tec_feng
 */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import javax.swing.*;

public class Test {

    private JFrame frame;
    private Container contentPane;
    private JTable jt1, jt2, jt3;
    private JTextField jf1, jf2, jf3;
    private JFileChooser chooser;
    private static String fileName;
    private JButton bt1, bt2, bt3, bt4, bt5;

    public Test() {
        frame = new JFrame("FlowLayout Test");
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initGUI();
    }

    public void initGUI() {
        contentPane = frame.getContentPane();
        contentPane.setLayout(new FlowLayout());
        chooser = new JFileChooser();
        jt1 = new JTable();
        jt1.setName("11111");
        jt2 = new JTable();
        jt3 = new JTable();

        jf1 = new JTextField();
        jf1.setColumns(20);
        jf2 = new JTextField();
        jf2.setColumns(20);
        jf3 = new JTextField();
        jf3.setColumns(20);
        bt1 = new JButton("需要複製的檔案");

        bt1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                chooser.setMultiSelectionEnabled(true);
                chooser.showOpenDialog(null);
             //   File[] files = chooser.getSelectedFiles();
              //  jf1.setText(chooser.getSelectedFile().getAbsolutePath());
             //   fileName = chooser.getSelectedFile().getName();
               
           //      String manyMess = "";
           //     for (File s : files) {
            //        manyMess = manyMess + s.getAbsolutePath() + ",";
             //   }
                jf1.setText(chooser.getSelectedFile().toString());
                fileName = chooser.getSelectedFile().getName();

            }
        });
        bt2 = new JButton("指定的檔案目錄");
        bt2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                chooser.showOpenDialog(null);
                jf2.setText(chooser.getSelectedFile().toString());

            }
        });
        bt3 = new JButton("  選擇 .bat 檔案  ");
        bt3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                chooser.setMultiSelectionEnabled(true);
                chooser.showOpenDialog(null);
                String manyMess = "";
                File[] files = chooser.getSelectedFiles();
                for (File s : files) {
                    manyMess = manyMess + s.getAbsolutePath() + ",";
                }
                jf3.setText(manyMess);
                fileName = chooser.getSelectedFile().getName();
            }
        });
        bt4 = new JButton("執行復制");
        bt4.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                InputStream inStream = null;
                FileOutputStream fs = null;
                try {
                    String s1 = jf1.getText();
                    String newPath = s1.replaceAll("\\\\", "\\\\\\\\");
                    String s2 = jf2.getText();
                    String oldPath = s2.replaceAll("\\\\", "\\\\\\\\");
                   
                //    String meString = jf1.getText().substring(0, jf1.getText().length() - 1);
                //    String[] meStrings = meString.split(",");
                   
                 //   for(int i=0;i<meStrings.length;i++){
                        int bytesum = 0;
                    int byteread = 0;
                    inStream = new FileInputStream(newPath); //讀入原檔案
                    fs = new FileOutputStream(oldPath + "\\" + fileName);
                    byte[] buffer = new byte[1444];
                    int length;
                    while ((byteread = inStream.read(buffer)) != -1) {
                        bytesum += byteread; //位元組數 檔案大小
                        fs.write(buffer, 0, byteread);
                //    }
                    }
                   
                    JOptionPane.showMessageDialog(null, "複製完成");
                    inStream.close();

                } catch (Exception es) {
                    System.out.println("複製單個檔案操作出錯");
                    es.printStackTrace();

                }

            }
        });

        bt5 = new JButton("  執行 .bat檔案  ");
        bt5.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String meString = jf3.getText().substring(0, jf3.getText().length() - 1);
                String[] meStrings = meString.split(",");
                for (int i = 0; i < meStrings.length; i++) {
                    String newString = meStrings[i].replaceAll("\\\\", "\\\\\\\\");
                    String cmd = "cmd /c start " + newString;

                    try {
                        Process ps = Runtime.getRuntime().exec(cmd);
                    } catch (Exception ioe) {
                        ioe.printStackTrace();
                    }
                }

            }
        });

        contentPane.add(jf1);
        contentPane.add(bt1);
        contentPane.add(jf2);
        contentPane.add(bt2);
        contentPane.add(jf3);
        contentPane.add(bt3);
        contentPane.add(bt4);
        contentPane.add(bt5);
    }

    public void go() {
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        new Test().go();
    }
}