Java实现目录拷贝

题目:选择输入流和输出流,实现目录拷贝。注意目的地的文件名

package recursivedirectory;

import static java.lang.System.out;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

class CopyWindow extends JFrame {

	private static final long serialVersionUID = 1L;
	private String currentPath;
	private String targetPath;
	private File currentFile;
	private File targetFile;

	private JButton one;
	private JButton two;
	BufferedWriter writer;// 读取文件
	BufferedReader reader;// 写入文件

	public CopyWindow(String currentPath, String targetPath) throws HeadlessException {
		super();
		this.currentPath = currentPath;
		this.targetPath = targetPath;
	}

	public void checkExists() {

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300, 200);
		setLocation(500, 300);
		Container c = getContentPane();
		currentFile = new File(currentPath);
		targetFile = new File(targetPath);
		if (!currentFile.exists()) {// 待拷贝文件目录不存在
			JOptionPane.showMessageDialog(null, "待拷贝路径不存在", "错误提示!", JOptionPane.ERROR_MESSAGE);
			return;
		}
		if (targetFile.exists()) {

			setTitle("目标文件已存在");

			one = new JButton("替换目标文件");
			two = new JButton("不替换,对文件重命名");
			Listen listen = new Listen();
			one.addActionListener(listen);
			two.addActionListener(listen);

			GridLayout gl = new GridLayout(2, 1, 5, 5);
			JPanel panelOne = new JPanel();
			panelOne.setLayout(gl);
			panelOne.add(one);
			panelOne.add(two);

			c.add(panelOne, "Center");
			setVisible(true);

			return;
		} else {
			createPath();
			makeDirectoryCopy(currentFile, targetFile);
			JOptionPane.showMessageDialog(null, "拷贝文件成功", "成功提示!", JOptionPane.INFORMATION_MESSAGE);
		}

	}

	class Listen implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();
			if (source == one) { // 如果得到的事件对象是OK按钮,ok按钮被点击
				DelectAndCreate();

			} else if (source == two) {
				RenameAndCreate();
			}
			JOptionPane.showMessageDialog(null, "拷贝文件成功", "成功提示!", JOptionPane.INFORMATION_MESSAGE);
			dispose();
		}

	}

	private void RenameAndCreate() {

		Rename();
		createPath();
		makeDirectoryCopy(currentFile, targetFile);
	}

	private void DelectAndCreate() {
		DeleteDirectory(targetPath);
		createPath();
		makeDirectoryCopy(currentFile, targetFile);
	}

	private void Rename() {
		int index = 1;
		while (targetFile.exists()) { // 判断目标文件是否已存在,如果存在添加后缀名
			targetPath += "(" + index + ")";
			++index;
			targetFile = null;
			targetFile = new File(targetPath); // 更新文件名
			targetPath = targetFile.getParent() + "\\" + currentFile.getName(); // 如果(1)已经存在,就改为(2),记录原文件名
		}
		targetPath = targetFile.getAbsolutePath();
	}

	private void createPath() {
		// 如果该目录还不存在,创建目录路径
		while (!targetFile.getParentFile().exists()) {
			if (!targetFile.getParentFile().mkdirs())
				out.println("创建文件夹失败");
		}
		targetFile.mkdirs();// 创建同名目录
	}

	// 两个文件的拷贝
	private void makeFileCopy(File currentFile, File targetFile) {
		createNewFile(targetFile);
		openTwoFiles(currentFile, targetFile);
		copy(currentFile, targetFile);
	}

	// 在复制目录中创建一个文件
	private void createNewFile(File targetFile) {
		try {
			targetFile.createNewFile();// 创建新的文件
		} catch (IOException e) {
			out.println("新建" + targetFile.getAbsolutePath() + "失败");
		}
	}

	// 打开原文件和刚刚创建的待复制文件
	private void openTwoFiles(File currentFile, File targetFile) {
		try {
			writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "utf-8"));// 写入目标文件
		} catch (IOException e) {
			out.println("写入" + targetFile.getAbsolutePath() + "失败");
		}
		try {
			reader = new BufferedReader(new FileReader(currentFile));// 读取当前文件
		} catch (IOException e) {
			out.println("读取" + currentFile.getAbsolutePath() + "失败");
		}
	}

	// 实现两个文件的内容拷贝
	private void copy(File currentFile, File targetFile) {
		try {
			String line = reader.readLine();
			while (line != null) {
				writer.write(line);
				writer.newLine();
				line = reader.readLine();
			}
		} catch (IOException e) {
			out.println("写入时失败");
		}
		// 关闭两个文件的输入和输出流
		try {
			writer.close();
		} catch (IOException e) {
			out.println("关闭" + currentFile.getAbsolutePath() + "文件失败");
		}
		try {
			reader.close();
		} catch (IOException e) {
			out.println("关闭" + targetFile.getAbsolutePath() + "文件失败");
		}
	}

	// 两个目录的拷贝
	public void makeDirectoryCopy(File currentFile, File targetFile) {
		if (currentFile.isDirectory()) { // 因为是递归复制,所以先判断原文件是不是目录
			File[] fileNames = currentFile.listFiles(); // 看看有哪些文件/文件夹
			for (File f : fileNames) {
				File dire = new File(targetFile.getAbsolutePath() + "\\" + f.getName());// 创建一个文件对象 指向
																						// 目标文件的绝对路径+新的文件名/目录
				if (f.isDirectory())// 如果f是原文件的子目录
					dire.mkdir(); // 在复制文件里创建一个文件夹
				makeDirectoryCopy(f, dire);// 拷贝两个子目录的内容

			}
		} else { // 如果传进来的是两个文件参数
			makeFileCopy(currentFile, targetFile);
		}
	}

	public void DeleteDirectory(String targetPath) {

		File f = new File(targetPath);
		File[] files = f.listFiles();
		for (int i = 0; i < files.length; i++) {
			if (files[i].isFile()) {
				try {
					files[i].delete(); // 删除子文件
				} catch (Exception e) {
					out.println("删除" + files[i].getAbsolutePath() + "失败");
					e.printStackTrace();
				}
			} else
				DeleteDirectory(files[i].getAbsolutePath());// 删除子目录
		}

		try {
			f.delete();// 删除当前目录
		} catch (Exception e) {
			out.println("删除" + f.getAbsolutePath() + "失败");
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		new CopyWindow("E:\\java\\Java实验报告", "E:\\copy\\Java实验报告").checkExists();
		System.out.println("E:\\copy\\Java实验报告".replaceAll("Java实验报告", "yes"));
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值