基于字节流和字符流实现的文件复制

package com.neusoft.mytest.myproj;  

002    

003 import java.io.*;  

004    

005 import java.io.File;  

006    

007 /**  

008  * @author 极致书生 基于字节流实现的文件的拷贝  

009  *   

010  */ 

011 public class ByteCopy {  

012    

013     public static void main(String[] args) {  

014         File f_s = new File("g:/wjl/jizhishusheng.txt"); // 要拷贝的文件对象  

015         File f_d = new File("D:/yxm"); // 要拷贝到的路径  

016         if (!f_d.exists()) { // 判断要拷贝到的路径是否存在,不存在就创建  

017             f_d.mkdirs();  

018    

019         }  

020         File f_dd = new File(f_d, "7s.txt");  

021         FileInputStream fis = null;  

022         FileOutputStream fos = null;  

023         try {  

024             fis = new FileInputStream(f_s);  

025             fos = new FileOutputStream(f_dd);  

026             byte[] byteTemp = new byte[1024];  

027             while(fis.read(byteTemp) != -1){  

028                 int size =byteTemp.length;  

029                 fos.write(byteTemp,0,size);  

030                    

031             }  

032         } catch (FileNotFoundException e) {  

033             e.printStackTrace();  

034         } catch (IOException e) {  

035             e.printStackTrace();  

036         }   

037         finally {  

038             try {  

039                 if (fis != null) {  

040                     fos.close();  

041    

042                 }  

043                 if (fos != null) {  

044                     fis.close();  

045    

046                 }  

047             } catch (IOException e) {  

048                 e.printStackTrace();  

049             }  

050    

051         }  

052    

053     }  

054    

055 }  

056    

057    

058 package com.neusoft.mytest.myproj;  

059    

060 import java.io.*;  

061    

062 import java.io.File;  

063    

064 /**  

065  * @author 极致书生 基于字符流实现的文件的拷贝  

066  *   

067  */ 

068 public class CharCopy {  

069    

070     public static void main(String[] args) {  

071         File f_s = new File("g:/wjl/jizhishusheng.txt"); // 要拷贝的文件对象  

072         File f_d = new File("D:/yxm"); // 要拷贝到的路径  

073         if (!f_d.exists()) { // 判断要拷贝到的路径是否存在,不存在就创建  

074             f_d.mkdirs();  

075    

076         }  

077         File f_dd = new File(f_d, "7s.txt");  

078         FileReader fis = null;  

079         FileWriter fos = null;  

080         try {  

081             fis = new FileReader(f_s);  

082             fos = new FileWriter(f_dd);  

083             char[] charTemp = new char[1];  

084             while (fis.read(charTemp) != -1) {  

085                 int size = charTemp.length;  

086                 fos.write(charTemp, 0, size);  

087             }  

088         } catch (FileNotFoundException e) {  

089             e.printStackTrace();  

090         } catch (IOException e) {  

091             e.printStackTrace();  

092         } finally {  

093             try {  

094                 if (fis != null) {  

095                     fos.close();  

096    

097                 }  

098                 if (fos != null) {  

099                     fis.close();  

100    

101                 }  

102             } catch (IOException e) {  

103                 e.printStackTrace();  

104             }  

105    

106         }  

107    

108     }  

109    

110 } 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值