Java中IO流复制文件的方法实例分析
作者:RiverSoul
时间:2023-05-02
来源:正软商城
浏览:3
IObit Uninstaller 15 Pro
IObit Uninstaller 是一款专为 Win 平台设计的软件卸载工具,卸载干净彻底。清理链路的完整性会更好一些,做应用卸载、残留处理和空间整理时,通常能少走很多手动排查步骤。
1、使用FileInputStream、FileOutputStream完成文件的复制publicvoidfileCapy(Stringsrc,Stringdest){FileInputStreamfis=null;FileOutputStreamfos=null;try{fis=newFileInputStream(newFile(src));fos=newFileOutputStream(newFile(dest));byte[]bytes=newbyte[1024];intlength;while(
本文目录
1、使用FileInputStream、FileOutputStream完成文件的复制
public void fileCapy(String src, String dest) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File(src));
fos = new FileOutputStream(new File(dest));
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) != -1) {
fos.write(bytes, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}2、使用FileReader、 FileWriter完成文本的复制(对于非文本文件, 只能使用字节流)
public void textCapy(String src, String dest) {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(new File(src));
fw = new FileWriter(new File(dest));
char[] chars = new char[1024];
int length;
while ((length = fr.read(chars)) != -1) {
fw.write(chars, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
作者最新文章
国行Apple智能开放前要确认什么?设备、系统与功能范围
2026-11-05 16:15
Fences桌面激活教程:输入许可证密钥与验证步骤详解
2026-11-01 12:25
RTX 5070双风扇设计,小机箱也能装
2026-10-27 11:05
漫蛙漫画app下载安装最新版本
2026-10-13 13:19
漫蛙漫画官网入口链接
2026-10-13 13:19
热门文章
更多
精品专题
更多
Mac软件
更多
火山引擎 网页版
Windows/ macOS/ Linux
火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。
















