IO拷贝代码:
一、文件字节流拷贝方式
1.字节流拷贝图片:
public static void copy() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("g:/dog.jpg");
fos = new FileOutputStream("g:/dog2.jpg");
System.out.println("图片正在拷贝");
int res = 0;
while ((res = fis.read()) != -1) {
fos.write(res);
}
System.out.println("图片拷贝成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != fos) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != fis) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.视频的字节流拷贝方式一(以单个字节形式进行拷贝)
public static void copyVideo1() {
System.out.println("以字节流方式进行拷贝,考完一个字节完成再拷贝另外一个,会特别慢,不建议");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("g:/111111.mp4");
fos = new FileOutputStream("g:/(111111).mp4");
System.out.println("正在以方式一拷贝");
int res = 0;
while ((res = fis.read()) != -1) {
fos.write(res);
}
System.out.println("拷贝成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != fos) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != fis) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.视频的字节流拷贝方式二(申请视频(文件)大小的一维数组缓冲区)
public static void copyVideo2() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("g:/111111.mp4");
fos = new FileOutputStream("g:/222222.mp4");
System.out.println("正在玩命地拷贝...");
int len = fis.available();
byte[] bArr = new byte[len];
fis.read(bArr);
fos.write(bArr);
System.out.println("拷贝文件成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != fos) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != fis) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.视频的字节流拷贝方式三(固定缓冲区大小,分多次拷贝)
public static void copyVideo3() {
long l1 = System.currentTimeMillis();
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("g:/111111.mp4");
fos = new FileOutputStream("g:/333333.mp4");
System.out.println("正在以第三种方式拷贝");
int res = 0;
byte[] bArr = new byte[1024];
while((res=fis.read(bArr)) != -1) {
fos.write(bArr, 0, res);
}
System.out.println("拷贝成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != fos) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != fis) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long l2 = System.currentTimeMillis();
System.out.println("拷贝时间为" + (l2 - l1));
}
二、文件字符流拷贝方式
1.文件字符流拷贝文件(只能处理文本文件)
public class FileCharCopyTest {
public static void copy() {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("g:/a.txt");
fw = new FileWriter("g:/b.txt");
System.out.println("文件正在拷贝");
int res = 0;
while((res = fr.read()) != -1) {
fw.write(res);
}
System.out.println("文件拷贝完成");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != fw) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != fr) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
FileCharCopyTest.copy();
}
}
三、缓冲字节流拷贝方式
1.视频的字节流拷贝方式四( 缓冲字节流拷贝视频(通常使用方式))
public static void copy1() {
long l1 = System.currentTimeMillis();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream("g:/111111.mp4"));
bos = new BufferedOutputStream(new FileOutputStream("g:/(111111).mp4"));
System.out.println("正在以缓冲流的方式进行拷贝");
byte[] bArr = new byte[1024];
int res = 0;
while ((res=bis.read(bArr)) != -1) {
bos.write(bArr, 0, res);
}
System.out.println("拷贝完成");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != bos) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != bis) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long l2 = System.currentTimeMillis();
System.out.println("拷贝时间为" + (l2 - l1));
}
四、缓冲字符流拷贝方式
1.缓冲字符流拷贝文件
public static void copy1() {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader("g:/a.txt"));
bw = new BufferedWriter(new FileWriter("g:/c.txt"));
System.out.println("正在拷贝");
String str = null;
while ((str=br.readLine()) != null) {
bw.write(str);
bw.newLine();
}
System.out.println("拷贝成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != bw) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}