文件的拷贝[Java]

输入流读取数据,输出流写入数据,在输入的同时,进行输出实现数据的拷贝


  • 输入流读取数据,输出流写入数据,在输入的同时,进行输出实现数据的拷贝

文件的拷贝

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package cn.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 文件拷贝:文件字节输入,输出流
*
* @author HQF
*
*/
public class Copy {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//1. 创建源 将abc拷贝到ab
File desFile = new File("abc");
File srcFile = new File("ab.txt");
//2. 选择流
OutputStream oStream = null;
InputStream iStream = null;

try {
oStream = new FileOutputStream(srcFile,true);
iStream = new FileInputStream(desFile);
//3. 操作(分段读取)
byte[] flush = new byte[1024];//缓冲容器
int len = -1;//接受长度
while((len = iStream.read(flush)) != -1) {
//字节数组-->字符串(解码)
oStream.write(flush,0,len);
}
oStream.flush();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//分别释放资源,先打开的后关闭
if (null != oStream) {
oStream.close();
}

if (package cn.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 文件拷贝:文件字节输入,输出流
*
* @author HQF
*
*/
public class Copy {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//1. 创建源 将abc拷贝到ab
File desFile = new File("abc");
File srcFile = new File("ab.txt");
//2. 选择流
OutputStream oStream = null;
InputStream iStream = null;

try {
oStream = new FileOutputStream(srcFile,true);
iStream = new FileInputStream(desFile);
//3. 操作(分段读取)
byte[] flush = new byte[1024];//缓冲容器
int len = -1;//接受长度
while((len = iStream.read(flush)) != -1) {
//字节数组-->字符串(解码)
oStream.write(flush,0,len);
}
oStream.flush();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//分别释放资源,先打开的后关闭
if (null != oStream) {
oStream.close();
}

if (null != iStream) {
iStream.close();
}
}
}

}


最后进行封装,复制任何文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package cn.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
* 文件拷贝:文件字节输入,输出流
*
* @author HQF
*
*/
public class Copy {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
copy("E:\\JAVA\\cn.shangxuetang\\image\\2.jpg", "2copy.jpg");
}

public static void copy(String srcStream,String destStream) throws IOException {
//1. 创建源
File desFile = new File(srcStream);//源头
File srcFile = new File(destStream);//目的地
//2. 选择流
OutputStream oStream = null;
InputStream iStream = null;

try {
oStream = new FileOutputStream(srcFile,true);
iStream = new FileInputStream(desFile);
//3. 操作(分段读取)
byte[] flush = new byte[1024];//缓冲容器
int len = -1;//接受长度
while((len = iStream.read(flush)) != -1) {
//字节数组-->字符串(解码)
oStream.write(flush,0,len);
}
oStream.flush();



} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//分别释放资源,先打开的后关闭
if (null != oStream) {
oStream.close();
}

if (package cn.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
* 文件拷贝:文件字节输入,输出流
*
* @author HQF
*
*/
public class Copy {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
copy("E:\\JAVA\\cn.shangxuetang\\image\\2.jpg", "2copy.jpg");
}

public static void copy(String srcStream,String destStream) throws IOException {
//1. 创建源
File desFile = new File(srcStream);//源头
File srcFile = new File(destStream);//目的地
//2. 选择流
OutputStream oStream = null;
InputStream iStream = null;

try {
oStream = new FileOutputStream(srcFile,true);
iStream = new FileInputStream(desFile);
//3. 操作(分段读取)
byte[] flush = new byte[1024];//缓冲容器
int len = -1;//接受长度
while((len = iStream.read(flush)) != -1) {
//字节数组-->字符串(解码)
oStream.write(flush,0,len);
}
oStream.flush();



} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//分别释放资源,先打开的后关闭
if (null != oStream) {
oStream.close();
}

if (null != iStream) {
iStream.close();
}
}
}
}