Java文件选择器与文件复制

// 一共三个类,分别为封装好后的MyFileChooser类,Botton类和测试类
package homework;
import java.awt.Container;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class MyFileChooser extends JFileChooser{

	public File fileChooser(Container c) { // 方法参数为容器变量
		FileNameExtensionFilter filter = new FileNameExtensionFilter( "JPG & GIF Images", "jpg", "gif");
		this.setFileFilter(filter);// 这两句用于至供选择图片,可以不要
		
		int x = this.showOpenDialog(c);// 定义int类型变量x用于储存用户的选择,如用户选择取消则返回值为1
		if(x == 1) {
			return null;
		}
		File f = this.getSelectedFile();// 定义File类型变量接受选择后的文件,并返回
		return f;
	}
}
package work;
import java.awt.Container;
import javax.swing.JButton;
public class MyJBotton extends JButton{
	public MyJBotton(String info,int x,int y,Container c) {
		this.setText(info);
		this.setBounds(x, y, 100, 20);
		c.add(this);
	}
}
package homework;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class MyTestFrame extends JFrame{
	// 定义一个全局变量f
	private File f; 

	public MyTestFrame() {
		this.setLayout(null); 
		
		MyJBotton b1 = new MyJBotton("选择文件", 200, 100, this);
		MyJBotton b2 = new MyJBotton("复制文件", 200, 200, this);
	
		b1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				MyFileChooser fc = new MyFileChooser();	// new一个MyFileChooser对象以便调用其方法			
				f = fc.fileChooser(MyTestFrame.this); // 调用选择方法后将对象引用传给f全局变量
				System.out.println(f.getAbsolutePath()); // 在控制台打印出源文件路径作为记录
			}
		});
		
		b2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {				
				fileSave(f);  // 调用文件复制方法
				JOptionPane.showMessageDialog(null, "复制成功!");
			}
		});
		
		
		this.setSize(500, 500);
		this.setVisible(true);
		this.setDefaultCloseOperation(3);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setTitle("TEST");
	}
	
	
	private void fileSave(File f) {
		InputStream in = null;
		OutputStream out = null;
		
		File fold = new File("copy"); // 在当前工程中创建新目录对象"copy"
		if(fold.exists() == false) {  // 如"copy"目录在当前工程中不存在则新建一个
			fold.mkdirs();
		}
		
		try {
			in = new FileInputStream(f.getAbsolutePath()); // 父类对象调用子类构造方法,传入源文件绝对地址,注意必须为文件绝对地址!
			out = new FileOutputStream("copy/" + f.getName()); // 父类对象调用子类构造方法,传入新地址,注意必须为文件绝对地址!
			
			byte[] by = new byte[10240]; // 采用byte数组来传输字节流
			int len = 0;
			
			while((len = in.read(by)) != -1) { // 每次从in中读取by存入内存中,并记录每次传输长度
				out.write(by, 0, len); // 三个参数分别为每次传输的byte长度,开始传输的长度,每次传输的长度,直到全部写入新文件中
			}			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}			
		}		
	}
	
	public static void main(String[] args) {
		MyTestFrame m = new MyTestFrame();
	}


}


猜你喜欢

转载自blog.csdn.net/ferdinand1993/article/details/80036235
今日推荐