package learning;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;
import javax.xml.crypto.Data;
public class IOStreamPractice {
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in);
String srcPath=sc.next();
String desPath=sc.next();
File srcFile=new File(srcPath);
File desFile=new File(desPath);
boolean b=FileUtil.copyImpl(srcFile,desFile);
System.out.println(b? "拷贝成功":"拷贝失败,请排查原因");
}
}
class FileUtil {
public static boolean copyImpl(File srcFile,File desFile) throws Exception{
if(!srcFile.exists()) return false;
if(!desFile.getParentFile().exists()) desFile.getParentFile().mkdirs();
if(srcFile.isFile()) {
return FileUtil.copyNormal(srcFile,desFile);
}
else {
return FileUtil.copyDir(srcFile,srcFile,desFile);
}
}
private static boolean copyNormal(File srcFile, File desFile) throws Exception{
if(!desFile.getParentFile().exists()) desFile.getParentFile().mkdirs();
InputStream input=new FileInputStream(srcFile);
OutputStream output=new FileOutputStream(desFile);
byte[] buffer=new byte[1024];
int len=0;
while( (len=input.read(buffer))!=-1 ) {
output.write(buffer,0,len);
}
return true;
}
private static boolean copyDir(File File,File srcFile, File desFile) throws Exception{
if(File.isDirectory()) {
File[] subFiles=File.listFiles();
if(subFiles!=null) {
for(int i=0;i<subFiles.length;i++) copyDir(subFiles[i], srcFile, desFile);
}
}
else {
String oldPath=File.getPath().replace(srcFile.getPath()+File.separator,"");
File newFile=new File(desFile.getPath(), oldPath);
copyNormal(File, newFile);
}
return true;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?