java如何通过字节流实现文件的拷贝
作者:FreshDream
时间:2023-04-29
来源:正软商城
浏览:0
通过字节流实现文件的拷贝/***通过字节流实现文件的拷贝*@paramsourcePath源文件路径*@paramtargetPath目标文件路径*/publicstaticvoidcopyFileByStream(StringsourcePath,StringtargetPath){//源文件路径Filesource=newFile(sourcePath);//目标文件路径Filetarget=newFile(targetPath);//如果源文件不存在则不能拷贝if(!source.exists())
本文目录
通过字节流实现文件的拷贝
/**
* 通过字节流实现文件的拷贝
* @param sourcePath 源文件路径
* @param targetPath 目标文件路径
*/
public static void copyFileByStream(String sourcePath,String targetPath){
//源文件路径
File source = new File(sourcePath);
//目标文件路径
File target = new File(targetPath);
//如果源文件不存在则不能拷贝
if(!source.exists()){
return;
}
//如果目标文件目录不存在则创建
if(!target.getParentFile().exists()){
target.getParentFile().mkdirs();
}
try {
//实现文件的拷贝
InputStream inputStream = new FileInputStream(source);
OutputStream outputStream = new FileOutputStream(target);
int temp = 0;
//每次读取1024个字节
byte[] data = new byte[1024];
//将每次读取的数据保存到字节数组里面,并且返回读取的个数
while ((temp = inputStream.read(data)) != -1){
//输出数组
outputStream.write(data,0,temp);
}
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
作者最新文章
mindmapper导出图片 图片保存设置
2026-11-03 23:44
Fences桌面分区不显示?重启资源管理器恢复Fences步骤详解
2026-11-01 22:42
navicat for MySQL安装教程 安装前准备事项
2026-10-29 19:57
3dmax不锈钢材质参数解析怎么操作
2026-10-26 15:25
photoshop路径选择工具在哪
2026-10-02 13:28
热门文章
更多
精品专题
更多
Mac软件
更多
火山引擎 网页版
Windows/ macOS/ Linux
火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。
















