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

📄 filecopydemo.java

📁 java课程的资料以及实验的代码
💻 JAVA
字号:
package FileCopy;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import javax.swing.JOptionPane;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class FileCopyDemo extends JFrame implements ActionListener{

	/**
	 * @param args
	 */
	JLabel lbl1;
	JLabel lbl2;
	JTextField jf1;
	JTextField jf2;
	JButton btn1;
	JButton btn2;
	public FileCopyDemo()
	{
		lbl1=new JLabel("SourcrDir");
		lbl2=new JLabel("destDir");
		jf1=new JTextField(20);
		jf2=new JTextField(20);
		btn1=new JButton("copy");
		btn2=new JButton("Cacel");
		
		btn1.addActionListener(this);
		btn2.addActionListener(this);
		JPanel jp=new JPanel();
		jp.setLayout(new GridLayout(3,2));
		
		jp.add(lbl1);
		jp.add(jf1);
		jp.add(lbl2);
		jp.add(jf2);
		jp.add(btn1);
		jp.add(btn2);
		
		this.getContentPane().add(jp);
		this.setSize(300,300);
		this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		Object obj=ae.getSource();
		if(obj==btn1)
		{
			String str1=jf1.getText();
			String str2=jf2.getText();
			//System.out.println(str1);
			copyFile(str1,str2,"java");
		}
	}
	public void copyFile(String source,String dest,String ext)
	{
		Filter extName=new Filter(ext);
		File fdir=new File(source);
		File []files=fdir.listFiles(extName);
		//System.out.println(files.length+"length");
		for(int i=0;i<files.length;i++)
		{
			System.out.println(files[i].getName());
			copy(files[i].toString(),dest+fdir.separator+files[i].getName());
		}
		JOptionPane.showMessageDialog(null, "Copy finished");
	}
	public void copy(String str1,String str2)
	{
		 try { 
	            byte[] buffer = new byte[1024]; 

	            // 来源文件
		File f1=new File(str1);
        if(!f1.exists())
        {
        	System.out.println("file not Exsits");
        }
        FileInputStream fileInputStream = 
            new FileInputStream(f1); 
        BufferedInputStream br=new BufferedInputStream(fileInputStream );
        DataInputStream dis=new DataInputStream(br);
        // 目的文件
        File f2=new File(str2);
        if(f2.exists())
        {
        	System.out.println("File have exsited");
        }
        FileOutputStream fileOutputStream = 
            new FileOutputStream(f2); 
        BufferedOutputStream  bos=new BufferedOutputStream(fileOutputStream);
        DataOutputStream dos=new DataOutputStream(bos);
        // available()可取得未读取的数据长度
        System.out.println("复制文件:" + 
                fileInputStream.available() + "字节"); 
       
        
        while(true) { 
            if(br.available() < 1024) { 
                // 剩余的数据比1024字节少
                // 一位一位读出再写入目的文件

                int remain = -1; 
                while((remain = br.read())
                                       != -1) {
                    dos.write(remain); 
                   dos.flush();
                }
                break; 
            } 
            else { 
                // 从来源文件读取数据至缓冲区 
                br.read(buffer); 
                // 将数组数据写入目的文件 
                dos.write(buffer); 
            } 
        } 

        // 关闭流 
    dis.close(); 
        dos.close(); 

        System.out.println("复制完成"); 
    } 
    catch(ArrayIndexOutOfBoundsException e) { 
        System.out.println(
                  "using: java FileStreamDemo src des"); 
        e.printStackTrace(); 
    } 
    catch(IOException e) { 
        e.printStackTrace(); 
    } 
}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
       new FileCopyDemo();
	}

}
class Filter implements FilenameFilter{
    String extent;
    Filter(String extent){
     this.extent=extent;
      }
      public boolean accept(File dir,String name){
      return name.endsWith("."+extent); //返回文件的后缀名
    }
    }

⌨️ 快捷键说明

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