io

  1. public class IOTest {  
  2.   
  3.     public static void main(String[] args) {  
  4.         IOTest io = new IOTest();  
  5.         try {  
  6.             long startTime = System.currentTimeMillis();  
  7.             io.readWrite("d:/file/aa.txt""d:/file/bb.txt");  
  8.             long endTime = System.currentTimeMillis();  
  9.             System.out.println("Time taken for reading and writing using default behaviour : "  
  10.                             + (endTime - startTime) + " milli seconds");  
  11.             long startTime1 = System.currentTimeMillis();  
  12.             io.readWriteBuffer("d:/file/aa.txt""d:/file/bb.txt");  
  13.             long endTime1 = System.currentTimeMillis();  
  14.             System.out.println("Time taken for reading and writing using buffered streams : "  
  15.                             + (endTime1 - startTime1) + " milli seconds");  
  16.             long startTime2 = System.currentTimeMillis();  
  17.             io.readWriteArray("d:/file/aa.txt""d:/file/bb.txt");  
  18.             long endTime2 = System.currentTimeMillis();  
  19.             System.out.println("Time taken for reading and writing using custom buffering : "  
  20.                             + (endTime2 - startTime2) + " milli seconds");  
  21.             long startTime3 = System.currentTimeMillis();  
  22.             io.readWriteCustom("d:/file/aa.txt""d:/file/bb.txt");  
  23.             long endTime3 = System.currentTimeMillis();  
  24.             System.out.println("Time taken for reading and writing using custom buffering : "  
  25.                             + (endTime3 - startTime3) + " milli seconds");  
  26.         } catch (IOException e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.     }  
  30.   
  31.     public static void readWrite(String fileFrom, String fileTo)  
  32.             throws IOException {  
  33.         InputStream in = null;  
  34.         OutputStream out = null;  
  35.         try {  
  36.             in = new FileInputStream(fileFrom);  
  37.             out = new FileOutputStream(fileTo);  
  38.             while (true) {  
  39.                 int bytedata = in.read();  
  40.                 if (bytedata == -1)  
  41.                     break;  
  42.                 out.write(bytedata);  
  43.             }  
  44.         }  
  45.         finally {  
  46.             if (in != null)  
  47.                 in.close();  
  48.             if (out != null)  
  49.                 out.close();  
  50.         }  
  51.     }  
  52.   
  53.     public static void readWriteBuffer(String fileFrom, String fileTo)  
  54.             throws IOException {  
  55.         InputStream inBuffer = null;  
  56.         OutputStream outBuffer = null;  
  57.         try {  
  58.             InputStream in = new FileInputStream(fileFrom);  
  59.             inBuffer = new BufferedInputStream(in);  
  60.             OutputStream out = new FileOutputStream(fileTo);  
  61.             outBuffer = new BufferedOutputStream(out);  
  62.             while (true) {  
  63.                 int bytedata = inBuffer.read();  
  64.                 if (bytedata == -1)  
  65.                     break;  
  66.                 out.write(bytedata);  
  67.             }  
  68.         }  
  69.         finally {  
  70.             if (inBuffer != null)  
  71.                 inBuffer.close();  
  72.             if (outBuffer != null)  
  73.                 outBuffer.close();  
  74.         }  
  75.     }  
  76.   
  77.     public static void readWriteArray(String fileFrom, String fileTo)  
  78.             throws IOException {  
  79.         InputStream in = null;  
  80.         OutputStream out = null;  
  81.         try {  
  82.             in = new FileInputStream(fileFrom);  
  83.             out = new FileOutputStream(fileTo);  
  84.             int availableLength = in.available();  
  85.             byte[] totalBytes = new byte[availableLength];  
  86.             int bytedata = in.read(totalBytes);  
  87.             out.write(totalBytes);  
  88.         }  
  89.         finally {  
  90.             if (in != null)  
  91.                 in.close();  
  92.             if (out != null)  
  93.                 out.close();  
  94.         }  
  95.     }  
  96.   
  97.     public static void readWriteCustom(String from, String to)  
  98.             throws IOException {  
  99.         InputStream in = new FileInputStream(from);  
  100.         OutputStream out = new FileOutputStream(to);  
  101.         try {  
  102.             byte[] bytes = new byte[128 * 1024];  
  103.             int len;  
  104.             while ((len = in.read(bytes)) > 0) {  
  105.                 out.write(bytes, 0, len);  
  106.             }  
  107.         } finally {  
  108.             if (in != null)  
  109.                 in.close();  
  110.             if (out != null)  
  111.                 out.close();  
  112.         }  
  113.     }  
  114. }  

 

 

 

 

 

 

 

 

 

 

 

 

 

     public   void  performCopying(){
        Writer writer
= null ;
        InputStream inputStream
= null ;
        
try  {
            writer
= new  FileWriter( " test.dat " );
            inputStream
= getClass().getResourceAsStream( " ./test.resource " );
            CopyUtils.copy(inputStream,writer);
        } 
catch  (IOException e) {
            e.printStackTrace();
        }
finally {
            IOUtils.closeQuietly(writer);
            IOUtils.closeQuietly(inputStream);
        }
    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值