BufferedInputStream方法:
BufferedOutputStream方法:
@Test
@DisplayName("每次读取1024个字节")
public void testFileReader() {
String inputImgPath = "src/main/resources/static/test1.jpg";
String outputImgPath = "src/main/resources/static/test1_copy.jpg";
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inputImgPath));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outputImgPath))
) {
byte[] buff = new byte[1024];
int readLength;
while ((readLength = bis.read(buff)) != -1) {
bos.write(buff, 0, readLength);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}