java如何通过字符缓冲流拷贝文件
作者:WeekendFlower
时间:2023-04-26
来源:正软商城
浏览:0
通过字符缓冲流拷贝文件字符缓冲流只能读取文本文件/***通过字符缓冲流实现文件的拷贝**@paramsourcePath源文件路径*@paramtargetPath目标文件路径*/publicstaticvoidcopyFileByBufferedChar(StringsourcePath,StringtargetPath){//源文件路径Filesource=newFile(sourcePath);//目标文件路径Filetarget=newFile(targetPath);//如果源文件不存在则不能拷
本文目录
通过字符缓冲流拷贝文件
字符缓冲流只能读取文本文件
/**
* 通过字符缓冲流实现文件的拷贝
*
* @param sourcePath 源文件路径
* @param targetPath 目标文件路径
*/
public static void copyFileByBufferedChar(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();
}
BufferedReader in = null;
BufferedWriter out = null;
try {
//字符缓冲输入流和字符缓冲输出流
in = new BufferedReader(new FileReader(source));
out = new BufferedWriter(new FileWriter(target));
//读取文件(每次读取一行)
String temp = null;
while((temp = in.readLine()) != null){
//输出到文件
out.write(temp);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//关闭流
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
作者最新文章
再漫画官网首页入口及在线观看方法
2026-11-05 09:42
ArchiveofOurOwn官网链接及同人文学阅读入口
2026-10-30 10:21
多张照片怎么合成PDF文件?三种图片转PDF工具怎么选?
2026-10-29 15:06
音述AI作品如何复制分享链接发给好友
2026-10-27 12:00
冰点还原怎么打开 程序启动步骤
2026-10-23 13:35
热门文章
更多
精品专题
更多
Mac软件
更多
火山引擎 网页版
Windows/ macOS/ Linux
火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。
















