Java中IO流复制文件的方法实例分析
作者:RiverSoul
时间:2023-05-02
浏览:3
IObit Uninstaller 15 Pro
IObit Uninstaller 是一款专为 Win 平台设计的软件卸载工具,卸载干净彻底。清理链路的完整性会更好一些,做应用卸载、残留处理和空间整理时,通常能少走很多手动排查步骤。
¥29
Windows
2026-08-22
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();
}
}
}
}
作者最新文章
Fences桌面激活教程:输入许可证密钥与验证步骤详解
2026-11-01 12:25
RTX 5070双风扇设计,小机箱也能装
2026-10-27 11:05
漫蛙漫画app下载安装最新版本
2026-10-13 13:19
漫蛙漫画官网入口链接
2026-10-13 13:19
漫蛙2漫画APP最新版本下载免费安装
2026-10-13 13:17
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多

































