首页 > 代码库 > Java 复制文件 (文件输入输出流)

Java 复制文件 (文件输入输出流)

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 CopyFileDemo {    public static void main(String[] args) {        copyFile("c:\\a.txt","d:\\b.txt");    }        private static final int OUTPUT_FILE_BUFFER_SIZE = 4*1024;        private static boolean copyFile(String srcdir,String desdir){         //文件流        FileInputStream fileInputStream = null;        FileOutputStream fileOutputStream = null;                //使用缓存        BufferedInputStream bis;        BufferedOutputStream bos;                //每次读取的长度        int readLength = 0;                //当前位置        int position = 0;                //打印信息        System.out.println("copyFile srcdir: " + srcdir + " desdir:" + desdir);        //目标文件        File disFileInfo = null;        //源文件信息        File fileInfo = new File(srcdir);        if(!fileInfo.exists()){            System.out.println("源文件不存在 返回false");            return false;        }                //定义缓存大小        byte[] buffer  = new byte[OUTPUT_FILE_BUFFER_SIZE];        try {            fileInputStream = new FileInputStream(fileInfo);        } catch (FileNotFoundException e) {            e.printStackTrace();            return false;        }        //缓冲的输入流        bis = new BufferedInputStream(fileInputStream, 0x4000);                //检测目标文件是否存在 创建文件        disFileInfo = new File(desdir);        if(!disFileInfo.exists()){            try {                disFileInfo.createNewFile();            } catch (IOException e) {                e.printStackTrace();                return false;            }        }                try {            fileOutputStream = new FileOutputStream(disFileInfo);        } catch (FileNotFoundException e) {            e.printStackTrace();            return false;        }        //缓冲的输出流        bos = new BufferedOutputStream(fileOutputStream, 0x10000);                while(position != fileInfo.length())        {            if(position != fileInfo.length()){                try {                    //{@                    /*核心代码*/                    //从此字节输入流中给定偏移量处开始将各字节读取到指定的 byte数组中buffer                    readLength = bis.read(buffer, 0, OUTPUT_FILE_BUFFER_SIZE);                    //将指定 byte数组buffer中从偏移量 off开始的 len个字节写入此缓冲的输出流                    bos.write(buffer, 0, readLength);                    position += readLength;                    //@}                } catch (IOException e) {                    e.printStackTrace();                    return false;                }            }        }                //依次关闭流        if(bis != null){            try {                bis.close();            } catch (IOException e) {                e.printStackTrace();                return false;            }        }                if(bos != null){            try {                bos.close();            } catch (IOException e) {                e.printStackTrace();                return false;            }        }                if(position == fileInfo.length()){            System.out.println("copyFile success !!");            return true;        }                return false;    }}
技术分享

Java 复制文件 (文件输入输出流)