java----复制文件的几种方式

来源:互联网 发布:始知官职为他人 编辑:程序博客网 时间:2024/05/22 00:36

1、用5方式复制文本文件

package frist;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


public class FiveFangFa {
public static void main(String[] args) throws IOException {
method1();
method2();
method3();
method4();
method5();
}


private static void method5() throws FileNotFoundException, IOException {
FileReader d1 = new FileReader("a.txt");
FileWriter d2 = new FileWriter("d.txt");
char[] chs3=new char[1024];
int len=0;
while((len=d1.read(chs3))!=-1){
d2.write(chs3,0,len);
d2.flush();
}
d1.close();
d2.close();
}


private static void method4() throws FileNotFoundException, IOException {
BufferedReader r1 = new BufferedReader(new FileReader("a.txt"));
BufferedWriter r2 = new BufferedWriter(new FileWriter("r.txt"));
char[] chs2=new char[1024];
int len=0;
while((len=r1.read(chs2))!=-1){
r2.write(chs2,0,len);
r2.close();
}
r1.close();
r2.close();
}


private static void method3() throws FileNotFoundException, IOException {
InputStreamReader s1 = new InputStreamReader(new FileInputStream(
"a.txt"));
OutputStreamWriter s2 = new OutputStreamWriter(new FileOutputStream(
"s.txt"));
char[] chs=new char[1024];
int len=0;
while((len=s1.read(chs))!=-1){
s2.write(chs,0,len);
s2.flush();
}
s1.close();
s2.close();
}


private static void method2() throws FileNotFoundException, IOException {
BufferedInputStream b1 = new BufferedInputStream(new FileInputStream(
"a.txt"));
BufferedOutputStream b2 = new BufferedOutputStream(
new FileOutputStream("c.txt"));
byte[] bys1 = new byte[1024];
int len = 0;
while ((len = b1.read(bys1)) != -1) {
b2.write(bys1, 0, len);
b2.flush();
}
b1.close();
b2.close();
}


private static void method1() throws FileNotFoundException, IOException {
FileInputStream f1 = new FileInputStream("a.txt");
FileOutputStream f2 = new FileOutputStream("b.txt");
byte[] bys=new byte[1024];
int len=0;
while((len=f1.read(bys))!=-1){
f2.write(bys,0,len);
f2.flush();
}
f1.close();
f2.close();
}
}

2、用4种方式复制图片

package two;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;


public class FourFangFa {
public static void main(String[] args) throws IOException {
method1();
method2();
method3();
method4();
}


private static void method4() throws FileNotFoundException, IOException {
FileInputStream s1 = new FileInputStream("e:\\高圆圆.jpg");
DataOutputStream s2 = new DataOutputStream(new FileOutputStream(
"f:\\copy4.jpg"));
byte[] bys=new byte[1024];
int len=0;
while((len=s1.read(bys))!=-1){
s2.write(bys,0,len);
}
s1.close();
s2.close();
}


private static void method3() throws FileNotFoundException, IOException {
FileInputStream p1 = new FileInputStream("e:\\高圆圆.jpg");
PrintStream p2 = new PrintStream(new FileOutputStream("f:\\copy3.jpg"),true);
byte[] bys=new byte[1024];
int len=0;
while((len=p1.read(bys))!=-1){
p2.write(bys,0,len);
}
p1.close();
p2.close();
}


private static void method2() throws FileNotFoundException, IOException {
BufferedInputStream b1 = new BufferedInputStream(new FileInputStream(
"e:\\高圆圆.jpg"));
BufferedOutputStream b2 = new BufferedOutputStream(
new FileOutputStream("f:\\copy1.jpg"));
byte[] bys=new byte[1024];
int len=0;
while((len=b1.read(bys))!=-1){
b2.write(bys,0,len);
b2.flush();
}
b1.close();
b2.close();
}


private static void method1() throws FileNotFoundException, IOException {
FileInputStream t1 = new FileInputStream("e:\\高圆圆.jpg");
FileOutputStream t2 = new FileOutputStream("f:\\copy.jpg");
byte[] bys=new byte[1024];
int len=0;
while((len=t1.read(bys))!=-1){
t2.write(bys,0,len);
t2.flush();
}
t1.close();
t2.close();
}
}