学习笔记之流文件操作01(拷贝文件)

public static void  copyFilePathName(String from,String toSrc) {
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
            inputStream = new FileInputStream(from);
            outputStream = new FileOutputStream(toSrc);
            int len  = 0; //指向文件的开头
            byte[] data = new byte[20];
            //一边读一边写文件
            while((len = inputStream.read(data))!=-1){
                outputStream.write(data,0,len);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (Exception e2) {
                        // TODO: handle exception
                        e2.printStackTrace();
                    }
                    
                }
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (Exception e2) {
                        // TODO: handle exception
                        e2.printStackTrace();
                        
                    }
                }
                    
            }
public static void main(String[] args) {
//        readFileByPathNaum(pathName);
        File file = new File("E:\\aa");
        file.mkdirs();
        copyFilePathName("E:\\test.txt","E:\\aa\\bb.txt");
}
        
    

猜你喜欢

转载自www.cnblogs.com/GuangMingDingFighter/p/9468346.html