一、浅谈io系统
io系统的结构化思想是:输入-转换流-装饰器-输出。
对于字节流来说,常见的结构类为:
package com.handchina.yunmart.middleware.service; import org.elasticsearch.common.inject.Inject; import org.junit.Test; import java.io.*; /** * Created by yq on 2016/11/18. */ public class IOTestServiceTest { @Inject private IOTestService ioTestService; @Test public void testFile() { FileReader br = null; FileWriter bw = null; try { String in = "file/fileDemoIn.txt"; String out = "file/fileDemoOut.txt"; br = new FileReader(in);int s; Long start1 = System.currentTimeMillis(); bw = new FileWriter(out); while ((s = br.read()) != -1) { bw.write(s); } System.out.println("===========================复制时间:" + (System.currentTimeMillis() - start1) + "毫秒"); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (bw != null) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } @Test public void testFileDemo() { BufferedReader br = null; BufferedWriter bw = null; try { String in = "file/fileDemoIn.txt"; String out = "file/fileDemoOut.txt"; br = new BufferedReader(new FileReader(in)); StringBuilder sb = new StringBuilder(); String s = null; Long start1 = System.currentTimeMillis(); while ((s = br.readLine()) != null) { sb.append(s); sb.append("\r\n"); } System.out.println("===========================读取时间:" + (System.currentTimeMillis() - start1) + "毫秒"); bw = new BufferedWriter(new FileWriter(out)); Long start2 = System.currentTimeMillis(); bw.write(sb.toString()); bw.flush(); System.out.println("===========================写入时间:" + (System.currentTimeMillis() - start2) + "毫秒"); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (bw != null) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } @Test public void testByte() { FileInputStream bi = null; FileOutputStream bo = null; try { String in = "file/fileStreamIn.avi"; String out = "file/fileStreamOut.avi"; bi = new FileInputStream(in); bo = new FileOutputStream(out); byte[] buf = new byte[1024]; Long start1 = System.currentTimeMillis(); int temp = 0; while ((temp = bi.read(buf)) != -1) { bo.write(buf, 0, temp); } System.out.println("===========================复制时间:" + (System.currentTimeMillis() - start1) + "毫秒"); } catch (IOException e) { e.printStackTrace(); } finally { if (bi != null) { try { bi.close(); } catch (IOException e) { e.printStackTrace(); } } if (bo != null) { try { bo.close(); } catch (IOException e) { e.printStackTrace(); } } } } @Test public void testByteDemo() { BufferedInputStream bi = null; BufferedOutputStream bo = null; try { String in = "file/fileStreamIn.avi"; String out = "file/fileStreamOut.avi"; bi = new BufferedInputStream(new FileInputStream(in)); bo = new BufferedOutputStream(new FileOutputStream(out)); byte[] buf = new byte[1024]; Long start1 = System.currentTimeMillis(); int temp = 0; while ((temp = bi.read(buf)) != -1) { bo.write(buf, 0, temp); } System.out.println("===========================复制时间:" + (System.currentTimeMillis() - start1) + "毫秒"); } catch (IOException e) { e.printStackTrace(); } finally { if (bi != null) { try { bi.close(); } catch (IOException e) { e.printStackTrace(); } } if (bo != null) { try { bo.close(); } catch (IOException e) { e.printStackTrace(); } } } } }