一、使用字节流复制图片

  1. public static void main(String[] args) throws IOException {
  2. FileInputStream fileInputStream = new FileInputStream("IMG_2185.PNG");
  3. FileOutputStream fileOutputStream = new FileOutputStream("img.jpg");
  4. byte[] buffer = new byte[1024];
  5. int len;
  6. while((len = fileInputStream.read(buffer)) != -1){
  7. fileOutputStream.write(buffer,0,len);
  8. }
  9. fileInputStream.close();
  10. fileOutputStream.close();
  11. }

image.png

二、使用字节流复制音频

  1. public static void main(String[] args) throws IOException {
  2. FileInputStream fileInputStream = new FileInputStream("1.mp3");
  3. FileOutputStream fileOutputStream = new FileOutputStream("2.mp3");
  4. byte[] buffer = new byte[1024];
  5. int len;
  6. while((len = fileInputStream.read(buffer)) != -1){
  7. fileOutputStream.write(buffer,0,len);
  8. }
  9. fileInputStream.close();
  10. fileOutputStream.close();
  11. }

三、使用字节流复制视频

  1. public static void main(String[] args) throws IOException {
  2. FileInputStream fileInputStream = new FileInputStream("lupin.mp4");
  3. FileOutputStream fileOutputStream = new FileOutputStream("lupin2.mp4");
  4. byte[] buffer = new byte[1024];
  5. int len;
  6. while((len = fileInputStream.read(buffer)) != -1){
  7. fileOutputStream.write(buffer,0,len);
  8. }
  9. fileInputStream.close();
  10. fileOutputStream.close();
  11. }