IO复制文件工具类

 

目录

最终效果图:

1.jar包主入口

2.Service

3.Dao

4.RandomUtil

5.jar包下载链接,附源码

6.最后不知道怎么打jar包小伙伴,参考链接


需求:读取input文件下的所有内容,并复制到output文件下。例如:

E:\input

E:\output

 

最终效果图:

 

1.jar包主入口

package CopyFile;

public class MainCopy {
	public static void main(String[] args) {
		CopyService.SendCopyPath();
	}
}

 

2.Service

package CopyFile;
import java.io.File;
import java.io.IOException;

public class CopyService {
	/***
     * 1.获取输入输出路径
     */
    public static void SendCopyPath() {
    	String separator = File.separator;
    	File directory = new File(""); 
    	String inputPath = directory.getAbsolutePath() + "/input";
    	String outputPath = directory.getAbsolutePath() + "/output";
    	
    	File inFile = new File(inputPath);
    	File outFile = new File(outputPath);
	System.out.println(inFile);
	System.out.println(outFile);
	
	try {
		getFile(inFile, outFile);
	} catch (IOException e) {
		System.out.println("文件创建失败,请检查路径");
	}
    }
    

	/**
	*  2.逻辑判断
	 * @param fileIn 输入路径o
	 * @param fileOut 输出路径
	 * @throws IOException 
	 */
	public static void getFile(File inFile, File outFile) throws IOException  {
		if(!inFile.exists()){
			inFile.mkdirs();
		}
		if(!outFile.exists()){
			outFile.mkdirs();
		}
		//1.得到input中所有的文件
		File[] listFiles = inFile.listFiles();
		if(listFiles.length>0){
			//2.遍历文件
			for (int i = 0; i < listFiles.length; i++) {
				//输出路径
				File inFile2 = listFiles[i];
				System.out.println("+输入:+++"+inFile2);
				//文件名
				String name = inFile2.getName();
				System.out.println("文件名====="+name);
				//输出路径
				File outFile2 = new File(outFile,name);
				System.out.println("-输出:---"+outFile2);
				
				//1.1文件--->遍历
				if(inFile2.isFile()){
					outFile2.createNewFile();
					CopyDao.CopyOldFile(name, inFile2, outFile2);
				}
				//1.2文件夹-->递归
				else{
					getFile(inFile2, outFile2);
				}
	}
	}else{
		System.out.println("空文件夹");
	}
	}
	
	
}

3.Dao

package CopyFile;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

public class CopyDao {
	/**
	 * 根据文件名判断
	 * @param name
	 * @param fileIn2
	 * @param fileOut2
	 */
	public static void CopyOldFile(String name, File fileIn2, File fileOut2) {
		//cpp处理逻辑
		if ((name.endsWith(".cpp") || name.endsWith(".CPP"))) {
			List<String> list = readToList(fileIn2);
			for (int i = 0; i < list.size(); i++) {
				System.out.println(list.get(i));
				if (list.get(i).startsWith("extern \"C\"")) {
					list.add(i, RandomString.CPPrandom());
					++i;
				}
			}
			writer(fileOut2, list);
			System.out.println(".cpp处理over");
		}

	//.h处理逻辑
	else if (name.endsWith(".h") || name.endsWith(".H")) {
		List<String> list = readToList(fileIn2);
		if (list.get(list.size() - 1).startsWith("@end")) {
			for (int i = 0; i < 200; i++) {
				list.add(list.size() - 1, RandomString.HRandom());
			}
		}
		writer(fileOut2, list);
		System.out.println(".h处理over");
	} 
	
	else if (name.endsWith(".m") || name.endsWith(".M")) {
		List<String> list = readToList(fileIn2);
		if (list.get(list.size() - 1).startsWith("@end")) {
			for (int i = 0; i < 200; i++) {
				list.add(list.size() - 1, RandomString.MRandom());
			}
		}
		writer(fileOut2, list);
		System.out.println(".m处理over");
	} 
	
	 else {
		writer(fileOut2, readToList(fileIn2));
	}

	}

	/**
	 * 2.读取文件--->放入集合
	 * @param fileIn2
	 * @return
	 */
	public static ArrayList<String> readToList(File fileIn2) {
		BufferedReader br = null;
		String line = null;
		ArrayList<String> list = new ArrayList<String>();
		StringBuffer buf = new StringBuffer();

	try {
		br = new BufferedReader(new InputStreamReader(new FileInputStream(fileIn2), "UTF-8"));

	while (((line = br.readLine()) != null)) {
		if (!line.equals("")) {
			buf.append(line);
			list.add(buf.toString());
			buf.delete(0, buf.length());
		}
	}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			br.close();

	} catch (IOException e) {
		e.printStackTrace();
	}
	}
	return list;
	}

	
	/**
	*	3.读取集合--->写出内容
	*/
	public static void writer(File fileOut, List<String> content) {
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileOut), "UTF-8"));
			for (int i = 0; i < content.size(); i++) {
				bw.write(content.get(i));
				bw.newLine();
			}

	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	}
}

4.RandomUtil

package CopyFile;

import java.util.LinkedList;
import java.util.Random;

public class RandomString {
	static LinkedList<String> llist = new LinkedList<String>();
	static int count=-1;
	/**
	 * .H文件拼接
	 * @return
	 */
	public static String HRandom() {
		String funcName = RString(15);
		llist.add(funcName);
		String sb = RString(10);
		StringBuffer sb2 = new StringBuffer();
		sb2.append("-(NSArray*)" + funcName + ":(NSArray*)" + sb + ";");
		return sb2.toString();
	}
	/**
	 * .M文件拼接
	 * @return
	 */
	public static String MRandom() {
		++count;
		String sb = RString(10);
		StringBuffer sb2 = new StringBuffer();
		for (int i = 0; i < llist.size(); i++) {
			sb2.append("-(NSArray*) " +llist.get(count)+ ":(NSArray*)" + sb + "{\r\n"+ "\treturn " + sb + ";\r\n}");
		}
		return sb2.toString();
	}
	
	
	/**
	  * .cpp
	 */
	public static String CPPrandom() {
		String s = RString(10);
		StringBuffer sb2 = new StringBuffer();
		sb2.append("extern \"C\"  void " + s + "(){\r\n" + "\treturn;\r\n}");
		return sb2.toString();
	}
	
	/**
	 * .py
	 */
	public static String PyRandom() {
		return RString(20);
	}

	/**
	 * 生成随机数
	 * @param length
	 * @return
	 */
	public static String RString(int length) {
		String str = "zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(52);
			sb.append(str.charAt(number));
		}
		return sb.toString();
	}
	
}

5.jar包下载链接,附源码

https://download.csdn.net/download/qq_42035966/10761537

 

6.最后不知道怎么打jar包小伙伴,参考链接

https://blog.csdn.net/qq_42035966/article/details/82086454

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值