- package com.kxg.Buffer;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class TestDemo {
- public static void main(String[] args) throws IOException {
- long start1 = System.currentTimeMillis();
- method1("a.txt", "1.txt");
- long end1 = System.currentTimeMillis();
- long time1 = (end1 - start1);
- long start2 = System.currentTimeMillis();
- method2("a.txt", "2.txt");
- long end2 = System.currentTimeMillis();
- long time2 = (end2 - start2);
- long start3 = System.currentTimeMillis();
- method3("a.txt", "3.txt");
- long end3 = System.currentTimeMillis();
- long time3 = (end3 - start3);
- long start4 = System.currentTimeMillis();
- method4("a.txt", "4.txt");
- long end4 = System.currentTimeMillis();
- long time4 = (end4 - start4);
- System.out.println("普通流一次一个字节:" + time1);
- System.out.println("普通流一次一个字节数组:" + time2);
- System.out.println("高效流一次一个字节:" + time3);
- System.out.println("高效流一次一个字节数组:" + time4);
- // 普通流一次一个字节:28725
- // 普通流一次一个字节数组:63
- // 高效流一次一个字节:59
- // 高效流一次一个字节数组:16
- }
- private static void method4(String srcString, String destString)
- throws IOException {
- // 高效字节流一次读写一个字节
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
- srcString));
- BufferedOutputStream bos = new BufferedOutputStream(
- new FileOutputStream(destString));
- byte[] by = new byte[1024];
- int len = 0;
- while ((len = bis.read(by)) != -1) {
- bos.write(by, 0, len);
- }
- }
- private static void method3(String srcString, String destString)
- throws IOException {
- // 高效字节流一次读写一个字节
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
- srcString));
- BufferedOutputStream bos = new BufferedOutputStream(
- new FileOutputStream(destString));
- int len = 0;
- while ((len = bis.read()) != -1) {
- bos.write(len);
- }
- }
- private static void method2(String srcString, String destString)
- throws IOException {
- // 一次读写一个字节数组
- FileInputStream fis = new FileInputStream(srcString);
- FileOutputStream fos = new FileOutputStream(destString);
- byte[] by = new byte[1024];
- int len = 0;
- while ((len = fis.read(by)) != -1) {
- fos.write(by, 0, len);
- }
- fis.close();
- fos.close();
- }
- private static void method1(String srcString, String destString)
- throws IOException {
- // 一次读写一个字节
- FileInputStream fis = new FileInputStream(srcString);
- FileOutputStream fos = new FileOutputStream(destString);
- int len = 0;
- while ((len = fis.read()) != -1) {
- fos.write(len);
- }
- fis.close();
- fos.close();
- }
- }
复制代码
|
|