1:创建字节输入流对象,用于读取文件
2:创建字节输出流对象,用于存储文件,注意第二个参数,是否追加
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("F:\\IdeaProjects\\Java-Api\\file\\1.txt");
fos = new FileOutputStream("F:\\IdeaProjects\\Java-Api\\file\\2.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
3:定义byte类型数组用于等下的read(byte[] bytes)方法读取
fis.read(bytes)
4.使用while循环从输入流读取一些字节数,并将它们存储到缓冲区byte中
while ((fis.read(bytes))!=-1){}
5:通过write(byte[] b, int off, int len)方法
fos.write(bytes,0,bytes.length);
6:关闭字节输出流对象,关闭字节输入流对象
finally {
//关闭输出流
try {
if (fos != null) {
fos.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}