Java 输入输出流总结

1. 运用BufferedInputStream 读取文件流和BufferedOutputStream写文件流:

Java代码 
  1. protected static void writeFile2(String inputPath, String outputPath) {  
  2.         BufferedInputStream bis = null;  
  3.         BufferedOutputStream bos = null;  
  4.         try {  
  5.             bis = new BufferedInputStream(new FileInputStream(inputPath));  
  6.             bos = new BufferedOutputStream(new FileOutputStream(outputPath));  
  7.             int buffer = 1024 * 1024;  
  8.             byte[] data = new byte[buffer];  
  9.             int len;  
  10.             while ((len = bis.read(data)) != -1) {  
  11.                 bos.write(data, 0, len);  
  12.             }  
  13.         } catch (Exception e) {  
  14.             e.printStackTrace();  
  15.         } finally {  
  16.             try {  
  17.                 if (bis != null) {  
  18.                     bis.close();  
  19.                 }  
  20.                 if (bos != null) {  
  21.                     bos.close();  
  22.                 }  
  23.             } catch (IOException e) {  
  24.             }  
  25.         }  
  26.     }  

 

2. BufferedReader 读取文件流和BufferedWriter写文件流:

Java代码 
  1. protected static String readFile(String inputPath) {  
  2.         BufferedReader br = null;  
  3.         try {  
  4.             br = new BufferedReader(new InputStreamReader(new FileInputStream(  
  5.                     inputPath)));  
  6.             StringBuffer result = new StringBuffer();  
  7.             String lineString = null;  
  8.             while ((lineString = br.readLine()) != null) {  
  9.                 result.append(lineString);  
  10.                 result.append("\n");  
  11.             }  
  12.             return result.toString();  
  13.   
  14.         } catch (IOException e) {  
  15.             e.printStackTrace();  
  16.             return null;  
  17.         } finally {  
  18.             try {  
  19.                 if (br != null) {  
  20.                     br.close();  
  21.                 }  
  22.             } catch (IOException e) {  
  23.             }  
  24.         }  
  25.   
  26.     }  

 

Java代码 
  1. protected static void writeFile(String inputString, String outputPath)  
  2.             {  
  3.         BufferedWriter bw = null;  
  4.         try {  
  5.             bw = new BufferedWriter(new FileWriter(outputPath));  
  6.             bw.write(inputString);  
  7.         } catch (IOException e) {  
  8.             e.printStackTrace();  
  9.         } finally {  
  10.             try {  
  11.                 if (bw != null) {  
  12.                     bw.close();  
  13.                 }  
  14.             } catch (IOException e) {  
  15.             }  
  16.         }  
  17.     }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值