频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

单文件到本地机文件夹的拷贝

来源: 作者: 出处:巧巧读书 2006-10-20 进入讨论组


package test2;


import java.awt.event.*;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.filechooser.FileFilter;
import javax.swing.JFileChooser;


/**
 * @author hx0105

 */
public class CopyFile extends JFrame {


 private javax.swing.JPanel jContentPane = null;


 private javax.swing.JButton jButton = null;
 private javax.swing.JTextField jTextField = null;
 private javax.swing.JTextField jTextField1 = null;
 private javax.swing.JButton jButton1 = null;
 private javax.swing.JButton jButton2 = null;
 /**
  * This is the default constructor
  */
 public CopyFile() {
  super();
  initialize();
 }
 /**
  * This method initializes this
  *
  * @return void
  */
 private void initialize() {
  this.setSize(300, 200);
  this.setContentPane(getJContentPane());
 }
 /**
  * This method initializes jContentPane
  *
  * @return javax.swing.JPanel
  */
 private javax.swing.JPanel getJContentPane() {
  if (jContentPane == null) {
   jContentPane = new javax.swing.JPanel();
   jContentPane.setLayout(null);
   jContentPane.add(getJButton(), null);
   jContentPane.add(getJTextField(), null);
   jContentPane.add(getJTextField1(), null);
   jContentPane.add(getJButton1(), null);
   jContentPane.add(getJButton2(), null);
  }
  return jContentPane;
 }
 /**
  * This method initializes jButton
  *
  * @return javax.swing.JButton
  */
 private javax.swing.JButton getJButton() {
  if(jButton == null) {
   jButton = new javax.swing.JButton();
   jButton.setBounds(206, 76, 80, 27);
   jButton.setText("copy");
   jButton.addActionListener(new java.awt.event.ActionListener()
   {
    public void actionPerformed(ActionEvent e)
    {
     String from = jTextField.getText();
     String to = jTextField1.getText();
     Copy copy1 = new Copy();
     boolean copy_ok = copy1.copy(from,to);
     if(copy_ok)
     {
      System.out.println("copy is successful!");
     }
     else
     {
      System.out.println("copy is failture!");
     }
    }
   }
   );
  }
  return jButton;
 }
 /**
  * This method initializes jTextField
  *
  * @return javax.swing.JTextField
  */
 private javax.swing.JTextField getJTextField() {
  if(jTextField == null) {
   jTextField = new javax.swing.JTextField();
   jTextField.setBounds(5, 50, 118, 31);
  }
  return jTextField;
 }
 /**
  * This method initializes jTextField1
  *
  * @return javax.swing.JTextField
  */
 private javax.swing.JTextField getJTextField1() {
  if(jTextField1 == null) {
   jTextField1 = new javax.swing.JTextField();
   jTextField1.setBounds(5, 96, 117, 28);
  }
  return jTextField1;
 }
 /**
  * This method initializes jButton1
  *
  * @return javax.swing.JButton
  */
 private javax.swing.JButton getJButton1() {
  if(jButton1 == null) {
   jButton1 = new javax.swing.JButton();
   jButton1.setBounds(125, 53, 76, 27);
   jButton1.setText("file");
   
   jButton1.addActionListener (new java.awt.event.ActionListener()
   {
    public void actionPerformed (ActionEvent e)
    {
     JFileChooser  fc = new JFileChooser();
     fc.setCurrentDirectory (new File("f:"));
    int returnVal = fc.showDialog(CopyFile.this, "OK");
    
    if (returnVal == JFileChooser.APPROVE_OPTION)
    {
     File file = fc.getSelectedFile();
     jTextField.setText(file.getPath());
    }
    else
    {
     jTextField.setText("f:");
    
    }
       }
   }
   );
  }
  return jButton1;
 }
 /**
  * This method initializes jButton2
  *
  * @return javax.swing.JButton
  */
 private javax.swing.JButton getJButton2() {
  if(jButton2 == null) {
   jButton2 = new javax.swing.JButton();
   jButton2.setBounds(126, 95, 78, 26);
   jButton2.setText("folder");
   jButton2.addActionListener(new java.awt.event.ActionListener()
         {
          public void actionPerformed (ActionEvent e)
          {
     JFileChooser fc = new JFileChooser();
              fc.setCurrentDirectory (new File ("f:"));
             int returnVal = fc.showDialog(CopyFile.this, "OK");
             if (returnVal == JFileChooser.APPROVE_OPTION)
             {
              File file = fc.getSelectedFile();
              jTextField1.setText(file.getPath());
             }
             else
             {
              jTextField1.setText("f:");
             }
       }
         });
  }
  return jButton2;
 }
}  //  @jve:visual-info  decl-index=0 visual-constraint="27,18"


 


//下面是拷贝文件用到的类


package test2;
import java.io.*;
import java.lang.Exception;
import java.util.*;


public class Copy
 {
public boolean copy(String file1,String file2)
{
try //must try and catch,otherwide will compile error
{
//instance the File as file_in and file_out
java.io.File file_in=new java.io.File(file1);
java.io.File file_out=new java.io.File(file2);
 FileInputStream in1=new FileInputStream(file_in);
 FileOutputStream out1=new FileOutputStream(file_out);
 byte[] bytes=new byte[1024];
 int c;
while((c=in1.read(bytes))!=-1)
out1.write(bytes,0,c);
 in1.close();
out1.close();
return(true); //if success then return true
 }


catch(Exception e)
{
System.out.println("Error!");
return(false); //if fail then return false
 }
 }
 }

  巧 巧 读 书:http://www.qqread.com/jsp/z245020.html进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
最新论坛文章
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章