java 文件复制,删除

package tms.com.TmsFile;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
 * 根据文件名称复制文件到指定文件夹下。
 * @author 2500216
 *
 */
public class TmsFile {

	/**
	 * all file cope
	 * 
	 * @param filePath
	 * @param targetH
	 * @param targetK
	 * @return
	 * @throws IOException
	 */
	public void copeAllFile(String filePath, String targetH, String targetK)
			throws IOException {

		File file = new File(filePath);
		System.out.println(file.getPath());
		if (file.isDirectory()) {
			String[] filelist = file.list();
			for (int i = 0; i < filelist.length; i++) {
				File readfile = new File(filePath + "\\" + filelist[i]);
				// 如果是文件
				if (!readfile.isDirectory()) {
                     
					String type = isType(readfile);

					if (type.equals("H")) {
						File targetFile = new File(targetH + readfile.getName());
						copyFile(readfile, targetFile);
					} else if (type.equals("K")) {
						File targetFile = new File(targetK + readfile.getName());
						copyFile(readfile, targetFile);
					}

				} else if (readfile.isDirectory()) {

					copeAllFile(readfile.getPath(), targetH, targetK);
				}
			}

		}

	}

	/**
	 * 判断是那个公司的
	 * 
	 * @param file
	 * @return
	 */
	public String isType(File file) {
		String type = file.getName().substring(0, 3);
		String typeResult = "";
		if ("KNA".equals(type)) {
			typeResult = "K";
		} else if ("KMH".equals(type)) {
			typeResult = "H";
		}
		return typeResult;
	}

	/**
	 * 把文件复制到指定路径下
	 * @param sourceFile
	 * @param targetFile
	 * @throws IOException
	 */
	public static void copyFile(File sourceFile, File targetFile)
			throws IOException {
		System.out.println("readfile   =" + sourceFile.getPath()
				+ "----->  targetFile =" + targetFile.getPath());
		BufferedInputStream inBuff = null;
		BufferedOutputStream outBuff = null;
		try {
			// 新建文件输入流并对它进行缓冲
			inBuff = new BufferedInputStream(new FileInputStream(sourceFile));

			// 新建文件输出流并对它进行缓冲
			outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));

			// 缓冲数组
			byte[] b = new byte[1024 * 5];
			int len;
			while ((len = inBuff.read(b)) != -1) {
				outBuff.write(b, 0, len);
			}
			// 刷新此缓冲的输出流
			outBuff.flush();
		} finally {
			// 关闭流
			if (inBuff != null)
				inBuff.close();
			if (outBuff != null)
				outBuff.close();
		}
	}

	/**
	 * 删除指定路径下的文件。
	 * 
	 * @param filePath
	 * @return
	 */
	public void deleteFile(File fs) {

		System.out.println("delete file name:" + fs.getName());
		fs.delete();
	}

	/**
	 * 删除文件
	 * 
	 * @param filePath
	 * @throws IOException
	 */
	public List<String> deleteAllFile(String filePath) throws IOException {
		List<String> fileList = new ArrayList<String>();
		File file = new File(filePath);
		System.out.println(file.getPath());
		if (file.isDirectory()) {
			String[] filelist = file.list();
			for (int i = 0; i < filelist.length; i++) {
				File readfile = new File(filePath + "\\" + filelist[i]);
				// 如果是文件
				if (!readfile.isDirectory()) {
					if (isDelete(readfile)) {
						fileList.add(readfile.getName() + "====>"
								+ readfile.getPath());
						deleteFile(readfile);
					}
				} else if (readfile.isDirectory()) {
					// 如果是目录:递归调用
					deleteAllFile(readfile.getPath());
				}
			}

		}
		return fileList;

	}

	/**
	 * 是否删除(删除文件名中是11月的)
	 * 
	 * @param file
	 * @return
	 */
	public boolean isDelete(File file) {
		boolean isDelete = false;
		String fileNameSub = file.getName().substring(22, 24);
		// System.out.println("fileNameSub="+fileNameSub);
		if (fileNameSub.equals("11")) {
			isDelete = true;
		} else {
			isDelete = false;
		}
		return isDelete;
	}

	/**
	 * 真实拷贝
	 * 
	 * @throws IOException
	 */
	public void startCope() throws IOException {
		// 真实路径

		String sourceFilePath = "E:\\AutoeverWorkFile\\tms\\sbs\\147\\log_vcrm\\";
		String targetH = "E:\\AutoeverWorkFile\\tms\\sbs\\H\\";
		String targetK = "E:\\AutoeverWorkFile\\tms\\sbs\\K\\";
		copeAllFile(sourceFilePath, targetH, targetK);
	}

	/**
	 * test拷贝
	 * 
	 * @throws IOException
	 */
	public void startCopeTest() throws IOException {
		String sourceFilePath = "E:\\AutoeverWorkFile\\tms\\sbs\\vcrmtest\\";
		String targetH = "E:\\AutoeverWorkFile\\tms\\sbs\\Htest\\";
		String targetK = "E:\\AutoeverWorkFile\\tms\\sbs\\Ktest\\";
		/*Map<String,String> targets=new HashMap();
		targets.put("H", targetH);
		targets.put("K", targetK);*/
		
		copeAllFile(sourceFilePath, targetH, targetK);
	}

	public static void main(String[] args) throws IOException {
		TmsFile tmsFile = new TmsFile();

		// **true
		// cope**************************************************************
		// tmsFile.startCope();

		// **test
		// cope**************************************************************
		//tmsFile.startCopeTest();

		// 删除11月份的。
		// tmsFile.deleteAllFile("E:\\AutoeverWorkFile\\tms\\sbs\\K\\");

		// ****************************************************************
		// 测试路径
		
		}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值