JAVA文件复制小程序

来源:互联网 发布:ios 读取网页数据 编辑:程序博客网 时间:2024/05/18 09:05

JAVA文件复制小程序

复制功能

  1. package tsc.function;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8.   
  9. /** 
  10.  * 文件复制功能 
  11.  * @author qiuyue 
  12.  * 
  13.  */  
  14.   
  15. public class CopyFile {  
  16.       
  17.     public static boolean copyFile(String source,String file) {  
  18.         int byteread = 0;  
  19.         InputStream in = null;  
  20.         OutputStream out = null;  
  21.           
  22.         try {  
  23.             in = new FileInputStream(source);  
  24.             out = new FileOutputStream(file);  
  25.             byte[] buffer = new byte[2048];  
  26.             while((byteread = in.read(buffer)) != -1) {  
  27.                 out.write(buffer, 0, byteread);  
  28.             }  
  29.             System.out.println("复制成功");  
  30.             return true;  
  31.         } catch (Exception e) {  
  32.             e.printStackTrace();  
  33.             return false;  
  34.         }finally {  
  35.             if(out != null) {  
  36.                 try {  
  37.                     out.close();  
  38.                 } catch (IOException e) {  
  39.                     e.printStackTrace();  
  40.                 }  
  41.             }  
  42.             if(in != null) {  
  43.                 try {  
  44.                     in.close();  
  45.                 } catch (IOException e) {  
  46.                     e.printStackTrace();  
  47.                 }  
  48.             }  
  49.         }  
  50.           
  51.           
  52.     }  
  53. }


前端

package tsc;    import java.awt.GridLayout;  import java.awt.event.ActionEvent;  import java.awt.event.ActionListener;  import java.util.Timer;    import javax.swing.*;    import tsc.function.MyTask.myTask;    public class MyFrame extends JFrame{        private static final long serialVersionUID = 371204056893587903L;        public MyFrame() {          this.setTitle("文件移动工具");          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          this.setSize(300, 150);          this.setLocationRelativeTo(null);          this.setResizable(false);          this.setLayout(new GridLayout(1, 2));          JButton start = new JButton("移动");          JButton exit = new JButton("退出");JFrame frame1 = new JFrame("运行提示");JFrame frame2 = new JFrame("运行提示");            start.addActionListener(new ActionListener() {              public void actionPerformed(ActionEvent e) {                  File file = new File(Data.SOURCE);if(file.exists()){CopyFile.copyFile(Data.SOURCE,Data.PATH);frame1.setLocationRelativeTo(null);frame1.setSize(240, 150);frame1.setVisible(true);JLabel label = new JLabel("移动成功",JLabel.CENTER);label.setFont(new java.awt.Font("Dialog",0,15));JButton quit  = new JButton("确定");frame1.getContentPane().add("South",quit);frame1.getContentPane().add("Center",label);quit.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0){frame1.setVisible(false);}});}else{frame2.setLocationRelativeTo(null);frame2.setSize(240,150);frame2.setVisible(true);JLabel label = new JLabel("移动失败,未找到文件",JLabel.CENTER);label.setFont(new java.awt.Font("Dialog",0,15));JButton quit  = new JButton("确定");frame1.getContentPane().add("South",quit);frame1.getContentPane().add("Center",label);quit.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0){frame2.setVisible(false);}});}            }          });          exit.addActionListener(new ActionListener() {              public void actionPerformed(ActionEvent e) {                  System.exit(0);              }          });          getContentPane().add(start);          getContentPane().add(exit);      }  }
Data

package tsc;    public class Data {        //源文件所在绝对路径      public static final String SOURCE = "E:/ceshi/Source/source.bin";        //转移到的绝对路径      public static final String PATH = "E:/ceshi/Test/source.bin"; 
Main

package tsc;    import tsc.MyFrame;    public class Main {        public static void main(String[] args) {          new MyFrame().setVisible(true);      }    }