文件流实现copy

package com.neuedu.File;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

public class TestCopy1 {

public static void main(String[] args) throws IOException {

//1.找到数据源

File fsr = new File("E:/timg.jpg");

//2.通过数据源获取文件名字

String filename = fsr.getName();

//3.通过名字在另一个空间生成新文件

File newFile = new File("D:/"+filename);

newFile.createNewFile();

//4.把源数据复制新文件中

//4.1 读取源数据(1次读一个,往新文件中写一个),一直读写到源文件结束

FileReader fr = new FileReader(fsr);

FileWriter fw = new FileWriter(newFile);

int a = fr.read();

while(a!=-1){

fw.write(a);

a = fr.read();

}

fw.flush();

}

package com.neuedu.File;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

public class TestCopy2 {

public static void main(String[] args) throws IOException {

//1.找到数据源

File fsr = new File("E:/11.mp4");

//2.通过数据源获取文件名字

String filename = fsr.getName();

//4.1 读取源数据(1次读一个,往新文件中写一个),一直读写到源文件结束

FileInputStream fr = new FileInputStream(fsr);

FileOutputStream fw = new FileOutputStream("D:/"+filename);

int a = fr.read();

while(a!=-1){

fw.write(a);

a = fr.read();

}

fw.flush();

}

}

package com.neuedu.File;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

public class TestCopy3 {

public static void main(String[] args) throws IOException {

//1.找到数据源

File fsr = new File("E:/11.mp4");

//2.通过数据源获取文件名字

String filename = fsr.getName();

//4.1 读取源数据(1次读一个,往新文件中写一个),一直读写到源文件结束

FileInputStream fr = new FileInputStream(fsr);

FileOutputStream fw = new FileOutputStream("D:/"+filename);

byte[] b = new byte[8192];

//高速缓存

int a = fr.read(b);

while(a!=-1){

fw.write(b);

a = fr.read(b);

}

fw.flush();

}

}

package com.neuedu.File;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class TestCopy4 {

public static void main(String[] args) throws IOException {

//1.找到数据源

File fsr = new File("E:/11.mp4");

//2.通过数据源获取文件名字

String filename = fsr.getName();

//4.1 读取源数据(1次读一个,往新文件中写一个),一直读写到源文件结束

FileInputStream fr = new FileInputStream(fsr);

FileOutputStream fw = new FileOutputStream("D:/"+filename);

BufferedInputStream bis = new  BufferedInputStream(fr);

BufferedOutputStream bos = new BufferedOutputStream(fw);

byte[] b = new byte[4096];

//高速缓存

int a = bis.read(b);

while(a!=-1){

bos.write(b,0,a);

a = bis.read(b);

}

bos.flush();

//3,303,390,133

//3,303,390,133

}

}

你可能感兴趣的:(文件流实现copy)