java如何通过字节缓冲流实现文件拷贝
作者:SunnyJourney
时间:2023-05-03
浏览:5
通过字节缓冲流实现文件拷贝/***通过字节缓冲流实现文件的拷贝**@paramsourcePath源文件路径*@paramtargetPath目标文件路径*/publicstaticvoidcopyFileByBuffered(StringsourcePath,StringtargetPath){//源文件路径Filesource=newFile(sourcePath);//目标文件路径Filetarget=newFile(targetPath);//如果源文件不存在则不能拷贝if(!source.exi
本文目录
通过字节缓冲流实现文件拷贝
/**
* 通过字节缓冲流实现文件的拷贝
*
* @param sourcePath 源文件路径
* @param targetPath 目标文件路径
*/
public static void copyFileByBuffered(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();
}
InputStream in = null;
OutputStream out = null;
try {
//字节缓冲输入流和字节缓冲输出流
in = new BufferedInputStream(new FileInputStream(source));
out = new BufferedOutputStream(new FileOutputStream(target));
byte[] b = new byte[1024];
int temp = 0;
//每次读取一个1024的字节数组
while((temp = in.read(b)) != -1){
//输出到文件
out.write(b,0,temp);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//关闭流
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
作者最新文章
CorelDRAW是免费软件吗?详解试用、订阅与一次性购买收费模式
2026-10-30 19:32
PDF怎么免费转成Word并保留内容?转换前后检查要点
2026-10-30 17:50
ArchiveofOurOwn小说快速入口链接
2026-10-30 10:30
冰点还原怎么安装 初始配置步骤
2026-10-28 13:38
漫蛙下载最新版本
2026-10-13 13:42
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多

































