注意:最后关闭流的时候,只需要关闭外层的缓冲流,因为会自动帮我们关闭内层的输入/输出流

    1. package test19;
    2. import java.io.*;
    3. /**
    4. * Created By Intellij IDEA
    5. *
    6. * @author Xinrui Yu
    7. * @date 2021/12/5 16:03 星期日
    8. */
    9. public class Application {
    10. public static void main(String[] args) {
    11. FileInputStream fileInputStream = null;
    12. FileOutputStream fileOutputStream = null;
    13. BufferedInputStream bufferedInputStream = null;
    14. BufferedOutputStream bufferedOutputStream = null;
    15. byte[] buffer = new byte[1024];
    16. try{
    17. fileInputStream = new FileInputStream("lupin.mp4");
    18. fileOutputStream = new FileOutputStream("lupin3.mp4");
    19. bufferedInputStream = new BufferedInputStream(fileInputStream);
    20. bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
    21. while((bufferedInputStream.read(buffer)) != -1){
    22. bufferedOutputStream.write(buffer);
    23. }
    24. }catch (IOException e){
    25. e.printStackTrace();
    26. }finally {
    27. if(bufferedInputStream != null){
    28. try {
    29. bufferedInputStream.close();
    30. } catch (IOException e) {
    31. e.printStackTrace();
    32. }
    33. }
    34. if(bufferedOutputStream != null){
    35. try {
    36. bufferedOutputStream.close();
    37. } catch (IOException e) {
    38. e.printStackTrace();
    39. }
    40. }
    41. }
    42. }
    43. }