Java io流完成复制粘贴功能

JAVA 中io字节输入输出流 完成复制粘贴功能:

    public static void main(String[] args) throws Exception{
        // 创建输入流要读取的文件
        FileInputStream  fis = new FileInputStream("f:yxx.txt");
        //创建要输出的文件路径
        FileOutputStream fos = new FileOutputStream("f:Yao/y.txt");
        //创建变量  用于接收 字节流读取返回字节
        int len = 0;
        while((len = fis.read())!=-1){
            fos.write(len);
        }
        //关闭数据流
        fis.close();
        fos.close();
    }

使用IO字节数组输入输出流  完成复制粘贴功能: 这个方法比较快

    // 创建输入流要读取的文件
        FileInputStream  fis = new FileInputStream("f:s.exe");
        //创建要输出的文件路径
        FileOutputStream fos = new FileOutputStream("f:Yao/s.exe");
        //创建变量  用于接收 字节流读取返回字节
        int len = 0;
        byte[] by = new byte[1024];
        while((len = fis.read(by))!=-1){
            fos.write(by,0,len);
        }
        //关闭数据流
        fis.close();
        fos.close();
    }

使用io流 字符流来复制  注意只能复制文本!!

/*
* 字符流的复制功能 只能复制 文本文件
* 循环一定要刷新缓冲区
* */
public static void main(String[] args)throws IOException {
FileReader fr = new FileReader("E:gu.txt");
FileWriter fw = new FileWriter("e:gubin/1.txt");

char[] cbuff = new char[1024];
int len = 0;
while ((len = fr.read(cbuff))!=-1) {
fw.write(cbuff);
fw.flush();
}
fr.close();
fw.close();
}

=====================


/*
* 文件复制方式,字节流,一共4个方式
* 1. 字节流读写单个字节 125250 毫秒
* 2. 字节流读写字节数组 193 毫秒 OK
* 3. 字节流缓冲区流读写单个字节 1210 毫秒
* 4. 字节流缓冲区流读写字节数组 73 毫秒 OK
*/
public class Copy {
public static void main(String[] args)throws IOException {
long s = System.currentTimeMillis();
copy_4(new File("c:\q.exe"), new File("d:\q.exe"));
long e = System.currentTimeMillis();
System.out.println(e-s);
}
/*
* 方法,实现文件复制
* 4. 字节流缓冲区流读写字节数组
*/
public static void copy_4(File src,File desc)throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
int len = 0 ;
byte[] bytes = new byte[1024];
while((len = bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
bos.close();
bis.close();
}
/*
* 方法,实现文件复制
* 3. 字节流缓冲区流读写单个字节
*/
public static void copy_3(File src,File desc)throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
int len = 0 ;
while((len = bis.read())!=-1){
bos.write(len);
}
bos.close();
bis.close();
}

/*
* 方法,实现文件复制
* 2. 字节流读写字节数组
*/
public static void copy_2(File src,File desc)throws IOException{
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(desc);
int len = 0 ;
byte[] bytes = new byte[1024];
while((len = fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fos.close();
fis.close();
}

/*
* 方法,实现文件复制
* 1. 字节流读写单个字节
*/
public static void copy_1(File src,File desc)throws IOException{
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(desc);
int len = 0 ;
while((len = fis.read())!=-1){
fos.write(len);
}
fos.close();
fis.close();
}
}

原文地址:https://www.cnblogs.com/gu-bin/p/10049293.html