Java中IO流复制文件的方法实例分析
作者:RiverSoul
时间:2023-04-28
浏览:2
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();
}
}
}
}
作者最新文章
Bandizip在Windows 10上安装失败怎么办?
2026-10-03 08:29
win10注册表修改怎么取消开机自动登录密码
2026-10-02 16:12
3dmax常见问题及解决办法:异常关闭和文件找回教程
2026-09-28 11:55
微软推出Project Zenith:面向Windows 11开发者的AI硬件加速方案
2026-09-08 18:15
打破流量垄断,让平台经济释放普惠红利
2026-09-08 18:07
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多










































