public class IO06 {
public static void main(String[] args) {
// ① 获取读取的目标文件
String sPath = SD.getPath(SD.D_W,"io","io02","100.txt");
File source = new File(sPath);
// ② 确定写入的目标文件
String tpah = SD.getPath(SD.D_W,"io","io02","111.txt");
File target = new File(tpah);
if (!target.exists()) {
try {
target.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
// ③ 创建对应文件的输入输出流
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(source);
out = new FileOutputStream(target);
// ④ 读取内容并写入文件
// 注:采用字节流进行多写操作,需借用byte[1024或其倍数].可以极大程度通过效率
// 一个字节读取一个字节写入handle exception
int res = 0;
while ((res = in.read()) != -1) {
out.write(res);
}
} catch (FileNotFoundException e) {
System.out.println("异常 - FileNotFoundException");
} catch (IOException e) {
System.out.println("异常 - IOException");
} finally {
// ⑤ 关闭流
try {
if (in != null)
in.close();
if (out != null)
out.close();
} catch (IOException e) {
System.out.println("异常 - IOException");
}
}
}
}
字节流 边读边写
最新推荐文章于 2023-08-18 09:04:05 发布