java如何通过字符流实现文件拷贝
作者:吹吹风会发光
时间:2023-04-24
来源:互联网
浏览:0
通过字符流实现文件拷贝使用字符流只能拷贝文本文件/***通过字符流实现文件的拷贝**@paramsourcePath源文件路径*@paramtargetPath目标文件路径*/publicstaticvoidcopyFileByReaderAndWriter(StringsourcePath,StringtargetPath){//源文件路径Filesource=newFile(sourcePath);//目标文件路径Filetarget=newFile(targetPath);//如果源文件不存在则不能
通过字符流实现文件拷贝
使用字符流只能拷贝文本文件
/**
* 通过字符流实现文件的拷贝
*
* @param sourcePath 源文件路径
* @param targetPath 目标文件路径
*/
public static void copyFileByReaderAndWriter(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();
}
FileReader in = null;
FileWriter out = null;
try {
//字符输入流和字符输出流
in = new FileReader(source);
out = new FileWriter(target);
char[] c = new char[1024];
int temp = 0;
//每次读取1024个字符
while ((temp = in.read(c)) != -1) {
//输出到文件
out.write(c, 0, temp);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
作者最新文章
PDF怎么取消密码保护?4种解锁方法整理
2026-09-08 18:23
三星 Galaxy Z Fold8 内屏边角支撑偏软?实测与官方回应
2026-09-08 16:39
手机Excel表格制作教程:小屏幕高效录入与格式调整指南
2026-09-04 09:27
PDF文档按页转换成图片怎么做?在线转换步骤整理
2026-09-03 11:06
PDF转Excel操作指南:在线工具、Adobe与Excel内置功能详解
2026-09-02 19:08
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















