程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> 單文件到本地機文件夾的拷貝

單文件到本地機文件夾的拷貝

編輯:JAVA編程入門知識

  

/*
 * 作成日: 2005/07/12
 *
 * この生成されたコメントの?啡毪丹欷毳匹螗抓飑`トを?涓?するため
 * ウィンドウ > ?O定 > Java > コ?`ド生成 > コ?`ドとコメント
 */
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
 *
 * この生成されたコメントの?啡毪丹欷毳匹螗抓飑`トを?涓?するため
 * ウィンドウ > ?O定 > Java > コ?`ド生成 > コ?`ドとコメント
 */
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
 }
 }
 }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved