单文件复制到指定目录和自动运行多个.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();
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值