⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 copyfileframe.java

📁 Java灵感编程1-101之41-60
💻 JAVA
字号:
package copyfile;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.borland.jbcl.layout.*;import java.io.*;/** * <p>Title: Copy File Demo</p> * <p>Description: This is a Copy File demo</p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: d6-125</p> * @author Liujun * @version 1.0 */public class CopyFileFrame extends JFrame {  JPanel contentPane;  XYLayout xYLayout1 = new XYLayout();  JButton jButton1 = new JButton();  JButton jButton2 = new JButton();  JTextArea jTextArea1 = new JTextArea();  JLabel jLabel1 = new JLabel();  //定义源文件集和目标文件目录  File [] source ;  File target = null;  //Construct the frame  public CopyFileFrame() {    enableEvents(AWTEvent.WINDOW_EVENT_MASK);    try {      jbInit();    }    catch(Exception e) {      e.printStackTrace();    }  }  //Component initialization  private void jbInit() throws Exception  {    //setIconImage(Toolkit.getDefaultToolkit().createImage(CopyFileFrame.class.getResource("[Your Icon]")));    contentPane = (JPanel) this.getContentPane();    jButton1.setText("选择源文件");    jButton1.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        jButton1_actionPerformed(e);      }    });    contentPane.setLayout(xYLayout1);    this.setSize(new Dimension(363, 219));    this.setTitle("Copy File Frame Demo");    jButton2.setText("选择目标文件");    jButton2.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        jButton2_actionPerformed(e);      }    });    jTextArea1.setBackground(Color.lightGray);    jTextArea1.setLineWrap(true);    contentPane.add(jTextArea1,      new XYConstraints(168, 11, 187, 147));    contentPane.add(jLabel1,      new XYConstraints(7, 160, 353, 26));    contentPane.add(jButton2, new XYConstraints(23, 89, 118, -1));    contentPane.add(jButton1, new XYConstraints(23, 31, 118, 28));  }  //Overridden so we can exit when window is closed  protected void processWindowEvent(WindowEvent e) {    super.processWindowEvent(e);    if (e.getID() == WindowEvent.WINDOW_CLOSING) {      System.exit(0);    }  }  void jButton1_actionPerformed(ActionEvent e) {      JFileChooser JFileCh = new JFileChooser();//创建文件对话框      JFileCh.setMultiSelectionEnabled(true);//设置可多选文件属性    int returnVal = JFileCh.showOpenDialog(this);//显示文件对话框    if(returnVal == JFileChooser.APPROVE_OPTION){       source = JFileCh.getSelectedFiles();//得到源文件集       jTextArea1.setText("选择了文件:\n");      for(int i=0;i<source.length;i++){//显示选择了的文件列表       jTextArea1.append(source[i].getPath()+"\n");       }//end for    }//end if    else        jTextArea1.setText("你没有选择文件");    }  void jButton2_actionPerformed(ActionEvent e) {    JFileChooser JFileCh = new JFileChooser();//创一个文件对话框选择目标文件    String filepath = null;    int returnVal = JFileCh.showSaveDialog(this);//显示文件对话框选择目标文件    if(returnVal == JFileChooser.APPROVE_OPTION){//当选择好目标文件后     target = JFileCh.getSelectedFile();//得到目标文件     filepath = target.getPath();//给filepath赋值为目标文件串     if(source !=null){       try{       if(source.length>1){//如果复制多个文件,则需要改变目标文件为输出目录        if(!target.isDirectory())target=target.getParentFile();       for(int k=0;k<source.length;k++){//根据输入的文件集,创建输入流           File NewFile1=new File(target,source[k].getName());       if(NewFile1.createNewFile()){          jLabel1.setText("正在复制文件:"+target.getPath()+"...");          FileInputStream FileIn=new FileInputStream(source[k]);//创建输入流          DataInputStream in = new DataInputStream(FileIn);          FileOutputStream fileout =  new FileOutputStream(NewFile1);//定义输出流          DataOutputStream out = new DataOutputStream(fileout);          int c;          while((c = in.read()) != -1)//写入流            out.write(c);          in.close();//关闭一个输入流          out.close();//关闭输出流,复制完成          JOptionPane.showMessageDialog(this, "文件复制成功:"+NewFile1.getPath());//例外,显示出错信息。          jLabel1.setText("文件复制成功:"+target.getPath());           }           else          jLabel1.setText("文件建立失败:"+target.getPath());           }//end for         }//end if Length >1        else{          jLabel1.setText("正在复制文件:"+target.getPath()+"...");          int   k=0;//只复制一个文件          FileInputStream FileIn=new FileInputStream(source[k]);          DataInputStream in = new DataInputStream(FileIn);          FileOutputStream fileout =  new FileOutputStream(target);//定义输出流          DataOutputStream out = new DataOutputStream(fileout);          int c;          while((c = in.read()) != -1)//写入流            out.write(c);          in.close();//关闭一个输入流          out.close();//关闭输出流,复制完成          jLabel1.setText("文件复制成功:"+target.getPath());          }//end else        }catch(Exception ei){          jLabel1.setText("文件复制失败:"+target.getPath());          }      }      else        jLabel1.setText("请先选择需要复制的文件");     }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -