利用文件输入流和输出流实现文件的复制,
下面的方法是:10字节10字节的复制,并且会把目标文件原来的内容给覆盖掉,如果目标文件原来有内容的话。
// 从指定的文件中读取内容,并写入到指定的文件内(相当于文件的复制)
@Test
public void testFileInputOutputStream() {
File src = new File("file/iotest.txt");//源文件
File des = new File("file/outputstream.txt");//目标文件
FileInputStream fis = null;//输入流,读
FileOutputStream fos = null;//输出流,写
try {
fis = new FileInputStream(src);//构造
fos = new FileOutputStream(des);
byte[] b = new byte[10];//字节数组
int len;
while ((len = fis.read(b)) != -1) {//从源文件中的输入流中读
fos.write(b, 0, len);//每读10字节就写进去,写到目标文件
//特别注意:这种写法将会把目标文件的原来内容给覆盖掉
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();//关闭写操作
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fis != null)
fis.close();//关闭读操作
} catch (IOException e) {
e.printStackTrace();
}
}
}
把上面的测试函数变成一个通用的函数就是:
public void copyFile(String src, String des) {
File file1 = new File(src);
File file2 = new File(des);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意这里没有判断文件是否存在,具体的可以参考上篇文章。
/**通过使用FileReader和FileWriter实现文件的复制
* Reader和Writer是一个字符流,也就是16bit
* @param src
* @param des
*/
public void copyFile(String src, String des) {
File file1 = new File(src);
File file2 = new File(des);
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(file1);//构造
fw = new FileWriter(file2);
char[] b = new char[1024];//字符数组
int len;
while ((len = fr.read(b)) != -1) {
fw.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**通过缓冲流实现文件的复制(可以调高文件复制的效率)
* 对于输出的缓冲流,写出的数据会先在内存中缓存,
* 使用flush()将会使内存中的数据立刻写出
*/
@Test
public void testBufferedInputStreamOutputStream() {
long start = System.currentTimeMillis();
// 1.
File file1 = new File("C:/Users/dell/Desktop/spider.rar");
File file2 = new File("file/1.rar");
// 2.
FileInputStream fis = null;//输入流
FileOutputStream fos = null;//输出流
BufferedInputStream bis = null;//缓冲输入流
BufferedOutputStream bos = null;//缓冲输出流
try {
fis = new FileInputStream(file1);//构造
fos = new FileOutputStream(file2);
// 3.
bis = new BufferedInputStream(fis);//缓冲流的构造
bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024];//字节数组,每次读取1024字节
int len;
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
bos.flush();//这句非常的重要,flush()将会使内存中的数据立刻写出
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(bos != null){
try {
bos.close();//关闭缓冲流时,会自动关闭它所包装的底层节点流
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long end = System.currentTimeMillis();
System.out.println("spend time:" + (end - start));
}
将上面的测试函数写成通用的函数:
public void copyFile(String src, String des) {
long start = System.currentTimeMillis();
// 1.
File file1 = new File(src);
File file2 = new File(des);
// 2.
FileInputStream fis = null;//输入流
FileOutputStream fos = null;//输出流
BufferedInputStream bis = null;//缓冲输入流
BufferedOutputStream bos = null;//缓冲输出流
try {
fis = new FileInputStream(file1);//构造
fos = new FileOutputStream(file2);
// 3.
bis = new BufferedInputStream(fis);//缓冲流的构造
bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024];//字节数组,每次读取1024字节
int len;
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
bos.flush();//这句非常的重要,flush()将会使内存中的数据立刻写出
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(bos != null){
try {
bos.close();//关闭缓冲流时,会自动关闭它所包装的底层节点流
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long end = System.currentTimeMillis();
System.out.println("spend time:" + (end - start));
}