从文件夹A复制指定类到文件夹B

从TXT中获取类列表,然后从文件夹rt中复制这些类,拷贝到文件夹rt1中

类列表类似:

sun/misc/JavaSecurityAccess 
java/security/AccessControlContext$1 
java/nio/charset/Charset$3 

工具类(转载自http://blog.csdn.net/lw001x/article/details/7745685 作者):

package com.ciotea.hosts;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class ReduceRt {
	// 文件拷贝
	public static boolean copy(String file1, String file2) {
		try // must try and catch,otherwide will compile error
		{
			// instance the File as file_in and file_out
			java.io.File file_in = new java.io.File(file1);
			java.io.File file_out = new java.io.File(file2);
			FileInputStream in1 = new FileInputStream(file_in);
			FileOutputStream out1 = new FileOutputStream(file_out);
			byte[] bytes = new byte[1024];
			int c;
			while ((c = in1.read(bytes)) != -1)
				out1.write(bytes, 0, c);
			in1.close();
			out1.close();
			return (true); // if success then return true
		} catch (Exception e) {
			System.out.println("Error!");
			return (false); // if fail then return false
		}
	}

	// 读取路径,copy
	public static int dealClass(String needfile, String sdir, String odir) throws IOException {
		int sn = 0; // 成功个数
		File usedclass = new File(needfile);
		if (usedclass.canRead()) {
			String line = null;
			LineNumberReader reader = new LineNumberReader(
					new InputStreamReader(new FileInputStream(usedclass), "UTF-8"));
			while ((line = reader.readLine()) != null) {
				line = line.trim();
				int dirpos = line.lastIndexOf("/");
				if (dirpos > 0) {
					String dir = odir + line.substring(0, dirpos);
					File fdir = new File(dir);
					if (!fdir.exists())
						fdir.mkdirs();
					String sf = sdir + line + ".class";
					String of = odir + line + ".class";
					boolean copy_ok = copy(sf.trim(), of.trim());
					if (copy_ok)
						sn++;
					else {
						System.out.println(line);
					}
				}
			}
		}
		return sn;
	}

	public static void main(String[] args) {
		String needfile = System.getProperty("user.dir")+"\\usedclass.txt";
		String sdir = System.getProperty("user.dir")+"/rt/";
		String odir = System.getProperty("user.dir")+"/rt1/";
		try {
			int sn = dealClass(needfile, sdir, odir);
			System.out.print(sn);
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
	}
}

转载于:https://my.oschina.net/u/2437172/blog/706217

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值