您的位置:首页 > 移动开发 > Objective-C

批量复制文件到指定目录并执行.bat文件(2012.06.06)

2012-06-06 16:58 113 查看
代码如下:

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package com.cn.text;

/**

 *

 * @author tec_feng

 */

import java.awt.*;

import java.awt.event.*;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import javax.swing.*;

import javax.swing.table.DefaultTableModel;

import javax.swing.table.TableColumn;

public class FileOperate {

    private JFrame frame;

    private Container contentPane;

    private JPanel jPanel;

    private JTable table, table1;

    private JButton jb1, jb2, jb3, jb4, jb5;

    private JFileChooser chooser;

    private static DefaultTableModel tableModel, tableModel1;

    public FileOperate() {

        frame = new JFrame("FileOperate");

        frame.setSize(713, 300);

        frame.setResizable(false);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        initGUI();

    }

    public void initGUI() {

        contentPane = frame.getContentPane();

        contentPane.setLayout(new BorderLayout());

        jPanel = new JPanel(new FlowLayout());

        Object row[][] = new Object[0][2];

        String[] tableHeadName = {"复制文件", "指定目录"};  //表头

        table = createTable(tableHeadName, row, tableModel);

        table.getColumnModel().getColumn(0).setMaxWidth(250);

        table.getColumnModel().getColumn(1).setMaxWidth(250);

        JScrollPane scrollPane = new JScrollPane(table);

        Object row1[][] = new Object[0][1];

        String[] tableHeadName1 = {"需执行的.bat文件"};  //表头

        table1 = createTable(tableHeadName1, row1, tableModel1);

        table1.getColumnModel().getColumn(0).setMaxWidth(250);

        JScrollPane scrollPane1 = new JScrollPane(table1);

 

        chooser = new JFileChooser();

        //设置宽度

        TableColumn firsetColumn = table.getColumnModel().getColumn(0);

        jb1 = new JButton("添加文件");

        jb1.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                addLister((DefaultTableModel) table.getModel());

            }

        });

        table.addMouseListener(getMouseListener());

        jb2 = new JButton("移除文件");

        jb2.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                if (-1 != table.getSelectedRow()) {

                    ((DefaultTableModel) table.getModel()).removeRow(table.getSelectedRow());

                }

                if (-1 != table1.getSelectedRow()) {

                    ((DefaultTableModel) table1.getModel()).removeRow(table1.getSelectedRow());

                }

            }

        });

        jb3 = new JButton("执行复制");

        jb3.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                FileInputStream inStream = null;

                FileOutputStream fs = null;

                try {

                    for (int i = 0; i < table.getRowCount(); i++) {

                        String s1 = (String) table.getModel().getValueAt(i, 0);

                        String[] fileNames = s1.split("\\\\");

                        String newPath = s1.replaceAll("\\\\", "\\\\\\\\");

                        String s2 = (String) table.getModel().getValueAt(i, 1);

                        String oldPath = s2.replaceAll("\\\\", "\\\\\\\\");

                        int bytesum = 0;

                        int byteread = 0;

                        inStream = new FileInputStream(newPath); //读入原文件

                        fs = new FileOutputStream(oldPath + "\\" + fileNames[fileNames.length - 1]);

                        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();

                    fs.close();

                } catch (Exception es) {

                    JOptionPane.showMessageDialog(null, "缺少目录或复制文件出错");

                    es.printStackTrace();

                }

            }

        });

        jb4 = new JButton("执行.bat文件");

        jb4.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                for (int i = 0; i < table1.getRowCount(); i++) {

                    String s1 = (String) table1.getModel().getValueAt(i, 0);

                    String newPath = s1.replaceAll("\\\\", "\\\\\\\\");

                    String cmd = "cmd /c start " + newPath;

                    try {

                        Process ps = Runtime.getRuntime().exec(newPath);

                    } catch (Exception ioe) {

                        ioe.printStackTrace();

                    }

                }

            }

        });

        jb5 = new JButton("添加.bat文件");

        jb5.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                addLister((DefaultTableModel) table1.getModel());

            }

        });

        jPanel.add(jb1);

        jPanel.add(jb5);

        jPanel.add(jb2);

        jPanel.add(jb3);

        jPanel.add(jb4);

        contentPane.add(scrollPane, BorderLayout.WEST);

        contentPane.add(scrollPane1, BorderLayout.CENTER);

        contentPane.add(jPanel, BorderLayout.SOUTH);

    }

    public void go() {

        frame.setVisible(true);

    }

    public void addLister(DefaultTableModel defaultTableModel) {

        int result;

        chooser.setMultiSelectionEnabled(true);

        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

        result = chooser.showOpenDialog(null);

        if (null != chooser.getSelectedFile() && result == JFileChooser.APPROVE_OPTION) {

            File[] files = chooser.getSelectedFiles();

            for (int i = 0; i < files.length; i++) {

                System.out.println(files[i]);

                defaultTableModel.addRow(new String[]{files[i].getAbsolutePath(), null});

            }

        }

        // 更新表格

        table.invalidate();

    }

    //创建表

    public JTable createTable(String[] tableHeadName, Object[][] rowAndCol, DefaultTableModel tableModel) {

        tableModel = new DefaultTableModel() {

            //设置表不可编辑

            @Override

            public boolean isCellEditable(int row, int column) {

                return false;

            }

        };

        tableModel.setDataVector(rowAndCol, tableHeadName);

        return new JTable(tableModel);

    }

    //监听鼠标事件

    public MouseListener getMouseListener() {

        return new MouseListener() {

            int status = 0;

            int row;

            int colunm;

            @Override

            public void mouseClicked(MouseEvent e) {

            }

            @Override

            public void mousePressed(MouseEvent e) {

                int result;

                if (e.getClickCount() == 2) {

                    chooser.setMultiSelectionEnabled(true);

                    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

                    result = chooser.showOpenDialog(null);

                    row = table.rowAtPoint(e.getPoint());

                    colunm = table.columnAtPoint(e.getPoint());

                    if (null != chooser.getSelectedFile() && result == JFileChooser.APPROVE_OPTION) {

                        table.getModel().setValueAt(chooser.getSelectedFile().getAbsolutePath().toString(), row, colunm);

                        //刷新table

                        table.updateUI();

                    }

                }

            }

            @Override

            public void mouseReleased(MouseEvent e) {

            }

            @Override

            public void mouseEntered(MouseEvent e) {

            }

            @Override

            public void mouseExited(MouseEvent e) {

            }

        };

    }

    public static void main(String args[]) {

        new FileOperate().go();

    }

}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐