JFrame实现文件管理

功能代码

@requires_authorization
public static void copyFile(File srcFile, File destFile){
		InputStream in = null;
		OutputStream out = null;
		try{
			in = new FileInputStream(srcFile);
		    out = new FileOutputStream(destFile);
		    byte[] b = new byte[1024];
		    int length = in.read(b); //返回有效字节数
		    	while(length != -1){
		    		out.write(b, 0, length);
		    		length = in.read(b);
		    }} catch(Exception e){
			e.printStackTrace();
		} finally{
			try {
				in.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

public class FileTool {

	// 拷贝
	public static void copyFile(String srcPath, String destPath, String srcWord, String destWord) {
		copyFile(new File(srcPath), new File(destPath), srcWord, destWord);
	}

	// 拷贝文件
	// 重载 用于将路径转化为文件
	// 添加关键字转化功能
	public static void copyFile(File srcFile, File destFile, String srcWord, String destWord) {
		InputStream in = null;
		OutputStream out = null;
		try {
			/*
			 * 1.流
			 */
			in = new FileInputStream(srcFile);
			out = new FileOutputStream(destFile);
			byte[] b = new byte[1024];
			int length = in.read(b); // 返回有效字节数
			/*
			 * 关键字转化 拷贝
			 */
			if (srcFile == null && "".equals(srcFile)) {
				// 不进行关键字转化 拷贝
				while (length != -1) {
					out.write(b, 0, length);
					length = in.read(b);
				}
			} else {// 进行关键字转化
					// 目标关键字为空则默认为""
				if (destWord == null) {
					destWord = "";
				}
				while (length != -1) {
					String string = new String(b, 0, length);// 获取0到length-1的字符串
					string = string.replace(srcWord, destWord);// 关键字替换
					out.write(b, 0, length);
					length = in.read(b);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

	// 拷贝文件夹
	public static void copyFolder(String srcPath, String destPath, String srcWord, String destWord) {
		copyFolder(new File(srcPath), new File(destPath), srcWord, destWord);
	}

	public static void copyFolder(File srcFile, File destFile, String srcWord, String destWord) {
		/**
		 * 1.是否存在
		 */
		if (srcFile != null && srcFile.exists()) {
			/**
			 * 2.是否文件夹
			 */
			if (srcFile.isDirectory()) {
				/**
				 * 3.目的文件夹是否存在
				 */
				// file流只能创建文件 不能创建文件夹
				if (!destFile.exists()) {
					destFile.mkdirs();
				}
				/** 4. 拿到文件夹里面的文件或文件夹的那个文件数组!!! **/
				File[] files = srcFile.listFiles();
				// 遍历递归
				for (File f : files) {
					String string = f.getName();
					File file = new File(destFile, string);
					copyFolder(f, file, srcWord, destWord);
				}
			} else {// 非文件夹则必定是文件
				copyFile(srcFile, destFile, srcWord, destWord);
			}

		}
	}

	// 删除文件
	public static void deleteFolder(String srcPath) {
		deleteFolder(new File(srcPath));
	}

	public static void deleteFolder(File srcFile) {
		/** 1.判断是否存在 **/
		if (srcFile.exists()) {
			/** 2.判断是否是文件夹 **/
			if (srcFile.isDirectory()) {
				File[] files = srcFile.listFiles();
				for (File f : files) {
					deleteFolder(f);
				}
			}
			srcFile.delete();// 文件 删除自己
		} else {
			srcFile.delete();
		}
	}

	public static void saveFile(String path, String content) {
		saveFile(new File(path), content);
	}

	public static void saveFile(File file, String content) {
		/**
		 * 1.判断父类文件夹是否存在
		 */
		if (!file.getParentFile().exists()) {
			file.mkdirs();
		}
		/**
		 * 2.写流 out
		 */
		OutputStream out = null;
		try {
			out = new FileOutputStream(file);
			out.write(content.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null)
					out.close();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				out = null;
			}
		}
	}}

带功能界面

package javaTEST;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

//增删查改界面
public class FunctionUI extends JFrame {
	static String Path1;// 原地址
	static String Path2;// 目的地址
	static String Path3;// 删除文件
	static String Path4;// 保存文件

	public void showUI() {
		// 界面
		setSize(515, 450);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(3);
		setLayout(null);

		/*
		 * 文件拷贝
		 */
		// 标签
		JLabel fileCopy = new JLabel("文件拷贝");
		JLabel src = new JLabel("源路径");
		JLabel dest = new JLabel("目标路径");
		fileCopy.setBounds(10, 12, 70, 20);
		src.setBounds(10, 40, 70, 20);
		dest.setBounds(10, 65, 70, 20);
		// 源路径
		JTextField srcPath = new JTextField();
		srcPath.setEditable(true);
		srcPath.setBounds(80, 40, 365, 20);
		// 目标路径
		JTextField destPath = new JTextField();
		destPath.setEditable(true);
		destPath.setBounds(80, 65, 365, 20);
		// 选择按钮
		JButton button1 = new JButton("...");
		button1.setBounds(450, 40, 30, 20);
		JButton button2 = new JButton("...");
		button2.setBounds(450, 65, 30, 20);
		// aaa转成bbb
		JTextField aaa = new JTextField();
		JTextField bbb = new JTextField();
		aaa.setBounds(10, 100, 200, 20);
		bbb.setBounds(280, 100, 200, 20);
		JLabel convertTo = new JLabel("转成");
		convertTo.setBounds(230, 100, 60, 20);
		// 拷贝按钮
		JButton copy = new JButton("确认拷贝");
		copy.setBounds(195, 135, 100, 20);
		// 添加到界面
		add(fileCopy);
		add(src);
		add(dest);
		add(srcPath);
		add(destPath);
		add(button1);
		add(button2);
		add(aaa);
		add(bbb);
		add(convertTo);
		add(copy);

		/*
		 * 文件删除
		 */
		// 标签 28 文件删除, 路径
		JLabel fileDelete = new JLabel("文件删除");
		fileDelete.setBounds(10, 172, 70, 20);
		JLabel path = new JLabel("路径");
		path.setBounds(10, 200, 70, 20);
		// 文本框: 路径
		JTextField route = new JTextField();
		route.setEditable(true);
		route.setBounds(80, 200, 365, 20);
		// 按钮: 选择,删除
		JButton button3 = new JButton("...");
		button3.setBounds(450, 200, 30, 20);
		JButton delete = new JButton("删除");
		delete.setBounds(195, 235, 100, 20);
		// 添加到界面
		add(fileDelete);
		add(path);
		add(route);
		add(button3);
		add(delete);

		/*
		 * 文件保存
		 */
		// 标签: 文件保存, 目标路径
		JLabel fileSave = new JLabel("文件保存");
		fileSave.setBounds(10, 277, 70, 20);
		JLabel destSave = new JLabel("目标路径");
		destSave.setBounds(10, 305, 70, 20);
		// 文本框: 目标路径, 输入框
		JTextField savePath = new JTextField();
		savePath.setBounds(80, 305, 365, 20);
		savePath.setEditable(true);
		JTextField enter = new JTextField();
		enter.setBounds(10, 330, 415, 20);
		enter.setEditable(true);
		// 按钮: 选择, 保存
		JButton button4 = new JButton("...");
		button4.setBounds(450, 305, 30, 20);
		JButton save = new JButton("保存");
		save.setBounds(430, 330, 60, 20);
		// 添加
		add(fileSave);
		add(destSave);
		add(savePath);
		add(enter);
		add(button4);
		add(save);

		setVisible(true);
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser j = new JFileChooser();
				j.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				j.showOpenDialog(null);
				// 设置后缀名
				FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt", "txt");
				j.setFileFilter(filter);
				Path1 = j.getSelectedFile().getAbsolutePath();
				srcPath.setText(Path1);
			}
		});

		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser j = new JFileChooser();
				j.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				j.showOpenDialog(null);
				Path2 = j.getSelectedFile().getAbsolutePath();
				destPath.setText(Path2);

			}

		});

		button3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser j = new JFileChooser();
				j.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				j.showOpenDialog(null);
				Path3 = j.getSelectedFile().getAbsolutePath();
				route.setText(Path3);

			}

		});

		button4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser j = new JFileChooser();
				j.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				j.showOpenDialog(null);
				Path4 = j.getSelectedFile().getAbsolutePath();
				savePath.setText(Path4);

			}

		});

		// 确认拷贝
		copy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String srcWord = aaa.getText();
				String destWord = bbb.getText();
				// FileTool ft = new FileTool();
				FileTool.copyFile(Path1, Path2, srcWord, destWord);
			}

		});

		// 删除
		delete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FileTool ft = new FileTool();
				Path3 = route.getText();
				ft.deleteFolder(Path3);
			}
		});

		// 保存
		save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FileTool ft = new FileTool();
				Path4 = savePath.getText();
				String content = enter.getText();
				ft.saveFile(Path4, content);
			}
		});
	}

	public static void main(String args[]) {
		FunctionUI fc = new FunctionUI();
		fc.showUI();
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值