栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java文件复制程序+Swing界面

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java文件复制程序+Swing界面

文件复制程序

实验目的:

1.熟练掌握Swing界面的编写;

2.学会使用JFileChooser文件选择器的使用;

3.学会使用文件输入流FileInputStream和输出流FileOutputStream复制文件。

实验内容:

        使用Swing基本组件和合适的布局管理器编写文件复制程序界面,使用JFileChooser文件选择器,实现源文件和目标路径的图形化操作,文件复制程序具体操作流程如下:

(1)程序运行主界面

                                     

图1  程序主界面

(2)单击“浏览”按钮,弹出文件选择界面,选择需要复制的源文件

                                    

图2 选择文件界面

    (3)同理操作,选择目标路径,然后单击“开始复制”完成文件复制

                                   

图3  文件复制成功

源代码:

import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
public class JFileChooserSimple extends Jframe {
	JLabel jl1 = new JLabel("源  文  件:");
	public JTextField jtf1 = new JTextField(25);
	JButton jb1 = new JButton("浏览");
	JLabel jl2 = new JLabel("目标路径:");
	public JTextField jtf2 = new JTextField(25);
	JButton jb2 = new JButton("浏览");
	JButton copyjb = new JButton("开始复制");
	public String fileName;
	public JFileChooserSimple() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		this.setTitle("文件复制程序");
		this.setLayout(new GridLayout(4, 1));
		this.setSize(500, 300);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);

		JPanel jp1 = new JPanel();
		jp1.add(jl1);
		jp1.add(jtf1);
		jp1.add(jb1);
		this.add(jp1);
		jb1.addMouseListener(new ButtonListener());

		JPanel jp2 = new JPanel();
		jp2.add(jl2);
		jp2.add(jtf2);
		jp2.add(jb2);
		this.add(jp2);
		jb2.addMouseListener(new ButtonListener1());
		JPanel jp3 = new JPanel();
		jp3.add(copyjb);
		this.add(jp3);
		copyjb.addMouseListener(new CopyFileSimple());
		
	}
	//以下是内部类的使用
	class ButtonListener extends MouseAdapter{
		
		@Override
		public void mouseClicked(MouseEvent e) {
			JFileChooser jc = new JFileChooser();
			int val = jc.showOpenDialog(null);
			if (val==JFileChooser.APPROVE_OPTION){
				String copyFromFilePath = jc.getSelectedFile().getAbsolutePath();
				jtf1.setText(copyFromFilePath);
			}else{
				jtf1.setText("没有选中文件!");
			}
			
		}
		
	}

	class ButtonListener1 extends MouseAdapter{
		
		@Override
		public void mouseClicked(MouseEvent e) {
			JFileChooser fc = new JFileChooser("F:\");
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int val = fc.showOpenDialog(null);
			if (val==fc.APPROVE_OPTION){
				String copyFromFilePath = fc.getSelectedFile().getPath();
				 fileName = fc.getSelectedFile().getName();
				jtf2.setText(copyFromFilePath);
			}else{
				jtf2.setText("没有选中文件夹!");
			}
			
		}
		
	}

	class CopyFileSimple extends MouseAdapter{
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		File c=new File(jtf1.getText());
		File d=new File(jtf2.getText()+"/"+fileName);
			try {
				FileReader in = new FileReader(c);
				FileWriter out = new FileWriter(d);
				int a;
				while ((a=in.read())!=-1)
					out.write(a);
				in.close();
				out.close();
			} catch (FileNotFoundException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		System.out.println("原来的文件位置"+c);
		System.out.println("复制生成的文件位置"+d);
		System.out.println("复制成功");
		
	}
			
	}
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException,IOException{
		JFileChooserSimple ad = new JFileChooserSimple();
		ad.setVisible(true);

	}
 }

以上程序只可实现farrago.txt到outagainc.txt的文本文件复制。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/666081.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号