- 【简】设计两个⽅法,分别实现拷⻉⼀个⽂本⽂件(分别⽤字节流和字符流实现)
字节流
private static boolean byteCopy(String srcPath,String dstPath){
// 1. 判断⽬标路径上,是否有⽂件存在
File dst = new File(dstPath);
if(dst.exists()){
return false;
}
// 2. 实现⽂件的拷⻉
try(InputStream inputStream = new FileInputStream(srcPath);
OutputStream outputStream = new FileOutputStream(dstPath)){
// 拷⻉的过程
// 1. 实例化⼀个字节数组
byte[] array = new byte[1024];
int length = 0;
//3. 循环读取数据
while((length = inputStream.read(array)) != -1){
// 2.4. 将读取到的数据,写⼊到输出流中
outputStream.write(array,0,length);
}
// 2.5. 冲刷缓冲区
outputStream.flush();
return true;
}catch (IOException e){
e.printStackTrace();
return false;
}
}
字符流
private static boolean charCopy(String srcPath,String dstPath){
try(Reader reader = new FileReader(srcPath);
Writer writer