拷贝图片
-
A:FileInputSteam读取
-
B:FileOutputStream写出
FileInputStream fis = new FileInputStream("致青春.mp3"); //创建输入流对象,关联致青春.mp3 FileOutputStream fos = new FileOutputStream("copy.mp3"); //创建输出流对象,关联copy.mp3 int b; while((b = fis.read()) != -1) { fos.write(b); } fis.close(); fos.close();
package com.heima.stream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo05_Copy {
/**
* @param 第一种拷贝
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//demo01();
FileInputStream fis = new FileInputStream("致青春.mp3");
FileOutputStream fos = new FileOutputStream("copy.mp3");
int b;
while ((b = fis.read()) != -1) {
fos.write(b);
}
fis.close();
fos.close();
}
private static void demo01() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("双元.jpg"); //创建输入流对象,关联双元.jpg
FileOutputStream fos = new FileOutputStream("copy.jpg"); //创建输出流对象,关联copy.jpg
int b;
while ((b = fis.read()) != -1) { //在不断的读取每个字节
fos.write(b); //将每个字节写出
}
fis.close(); //关流释放资源
fos.close();
}
}