黑马程序员技术交流社区
标题:
IO 字节流复制图片、视频文件的四种方式
[打印本页]
作者:
marcojam
时间:
2015-8-8 13:16
标题:
IO 字节流复制图片、视频文件的四种方式
package copyImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyImageDemo {
public static void main(String[] args) throws IOException {
File stri = new File("e:\地质灾害隐患分布图.jpg");//源文件对象
File stro = new File("f:\地质灾害隐患分布图.jpg");//输出文件对象
//bytecopy(stri, stro);
//bytecopy1(stri, stro);
//bufferedbytecopy(stri, stro);
bufferedbytecopy1(stri, stro);
}
// 方式一:基本字节流
public static void bytecopy(File stri, File stro) throws IOException {
FileInputStream fis = new FileInputStream(stri);
FileOutputStream fos = new FileOutputStream(stro);
int b = 0;
while ((b = fis.read()) != -1) {
fos.write(b);
}
fos.close();
fis.close();
}
// 方式二:基本字节流数组流
public static void bytecopy1(File stri, File stro) throws IOException {
FileInputStream fis = new FileInputStream(stri);
FileOutputStream fos = new FileOutputStream(stro);
int b = 0;
byte[] by = new byte[1024];
while ((b = fis.read(by)) != -1) {
fos.write(by, 0, b);
}
fos.close();
fis.close();
}
// 高效字节缓冲流
public static void bufferedbytecopy(File stri, File stro)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
stri));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(stro));
int b = 0;
while ((b = bis.read()) != -1) {
bos.write(b);
}
bos.close();
bis.close();
}
// 高效字节数组缓冲流
public static void bufferedbytecopy1(File stri, File stro)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
stri));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(stro));
int b = 0;
byte[] by = new byte[1024];
while ((b = bis.read(by)) != -1) {
bos.write(by, 0, b);
}
bos.close();
bis.close();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2