复制Java项目中的特定源文件(少量文件)

一、需求说明

需求原因:项目由于某种原因不能提交SVN,需将已经写好的源码给另一个同事,不想每次都去创建目录

操作系统:Windows

IDE:Eclipse

需求:Eclipse下直接使用Copy Qualified Name 得到值后,粘贴在配置文件中后运行程序即可实现复制文件(没有文件夹自动创建)

性能:无性能要求,能用就行

二、Jar包依赖

依赖包下载地址:

https://repo1.maven.org/maven2/commons-io/commons-io/2.5/commons-io-2.5.jar

		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.5</version>
		</dependency>

 

三、代码实现

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class SimpleCopyJavaFile {
	
	public static void main(String[] args) throws IOException {
		String userName = "你的Windows系统用户名";
		String destParentPath = "C:/Users/"+userName+"/Desktop/temp";
		String srcParentPath = "D:/workspace";		
		String configPath = "C:/Users/"+userName+"/Desktop/1.txt";
		copyFile(configPath, destParentPath, srcParentPath);
		System.out.println("done");
	}
	
	/**
	 * 导出文件夹处理(如不存在则创建)
	 * @param parentPath   父路径(如D:/workspace)
	 * @param childrenPath  子路径(文件名,如1.txt)
	 * @return
	 */
	private static File createFolder(String parentPath,String childrenPath){
		StringBuilder builder = new StringBuilder();
		builder.append(parentPath)
		.append(childrenPath);
		File file = new File(builder.toString());
		File parentFile = file.getParentFile();
		if(!parentFile.exists()){
			parentFile.mkdirs();
		}
		return file;
	}

	/**
	 * 复制文件
	 * @param configPath  控制导出哪个文件的配置文件
	 * @param destParentPath   导出父路径
	 * @param srcParentPath    项目所在的路径(待优化)
	 * @throws IOException
	 */
	private static void copyFile(String configPath,String destParentPath,String srcParentPath) throws IOException{
		String childrenPath;
		FileReader reader = new FileReader(configPath);
		BufferedReader bf = new BufferedReader(reader);
		while((childrenPath = bf.readLine())!=null){
			if(childrenPath != null && childrenPath.length() > 0){
				File destFile = createFolder(destParentPath,childrenPath);
				String srcPath = srcParentPath + childrenPath;
				File srcFile = new File(srcPath);
				FileUtils.copyFile(srcFile,destFile);
			}
		}
		bf.close();
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java文件操作类来实现将一个源文件复制到目标文件夹的功能。具体步骤如下: 1. 使用File类创建源文件和目标文件夹的File对象。 2. 使用FileInputStream类创建源文件的输入流对象,使用FileOutputStream类创建目标文件的输出流对象。 3. 通过读取源文件的输入流,将源文件的内容写入目标文件的输出流,完成文件复制操作。 4. 关闭输入流和输出流,释放资源。 下面是Java代码示例: ``` import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopyExample { public static void main(String[] args) { // 源文件路径 File sourceFile = new File("source.txt"); // 目标文件夹路径 File targetFolder = new File("target"); // 目标文件路径 File targetFile = new File(targetFolder, sourceFile.getName()); try { // 创建目标文件夹 targetFolder.mkdirs(); // 创建输入流和输出流对象 FileInputStream in = new FileInputStream(sourceFile); FileOutputStream out = new FileOutputStream(targetFile); // 复制文件 byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } // 关闭输入流和输出流 in.close(); out.close(); System.out.println("文件复制成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上述代码,我们首先创建了源文件和目标文件夹的File对象,然后通过FileInputStream类创建源文件的输入流对象,FileOutputStream类创建目标文件的输出流对象。在复制文件的过程,我们使用了一个大小为1024字节的缓冲区,每次读取源文件的1024字节内容,然后将其写入目标文件的输出流。最后,我们关闭了输入流和输出流,并在控制台输出了复制成功的提示信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值