io流效率的比较

public class IOTest {

public static void main(String[] args) {
IOTest io = new IOTest();
try {
long startTime = System.currentTimeMillis();
io.readWrite("d:/file/aa.txt", "d:/file/bb.txt");
long endTime = System.currentTimeMillis();
System.out.println("Time taken for reading and writing using default behaviour : "
+ (endTime - startTime) + " milli seconds");
long startTime1 = System.currentTimeMillis();
io.readWriteBuffer("d:/file/aa.txt", "d:/file/bb.txt");
long endTime1 = System.currentTimeMillis();
System.out.println("Time taken for reading and writing using buffered streams : "
+ (endTime1 - startTime1) + " milli seconds");
long startTime2 = System.currentTimeMillis();
io.readWriteArray("d:/file/aa.txt", "d:/file/bb.txt");
long endTime2 = System.currentTimeMillis();
System.out.println("Time taken for reading and writing using custom buffering : "
+ (endTime2 - startTime2) + " milli seconds");
long startTime3 = System.currentTimeMillis();
io.readWriteCustom("d:/file/aa.txt", "d:/file/bb.txt");
long endTime3 = System.currentTimeMillis();
System.out.println("Time taken for reading and writing using custom buffering : "
+ (endTime3 - startTime3) + " milli seconds");
} catch (IOException e) {
e.printStackTrace();
}
}

public static void readWrite(String fileFrom, String fileTo)
throws IOException {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(fileFrom);
out = new FileOutputStream(fileTo);
while (true) {
int bytedata = in.read();
if (bytedata == -1)
break;
out.write(bytedata);
}
}
finally {
if (in != null)
in.close();
if (out != null)
out.close();
}
}

public static void readWriteBuffer(String fileFrom, String fileTo)
throws IOException {
InputStream inBuffer = null;
OutputStream outBuffer = null;
try {
InputStream in = new FileInputStream(fileFrom);
inBuffer = new BufferedInputStream(in);
OutputStream out = new FileOutputStream(fileTo);
outBuffer = new BufferedOutputStream(out);
while (true) {
int bytedata = inBuffer.read();
if (bytedata == -1)
break;
out.write(bytedata);
}
}
finally {
if (inBuffer != null)
inBuffer.close();
if (outBuffer != null)
outBuffer.close();
}
}

public static void readWriteArray(String fileFrom, String fileTo)
throws IOException {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(fileFrom);
out = new FileOutputStream(fileTo);
int availableLength = in.available();
byte[] totalBytes = new byte[availableLength];
int bytedata = in.read(totalBytes);
out.write(totalBytes);
}
finally {
if (in != null)
in.close();
if (out != null)
out.close();
}
}

public static void readWriteCustom(String from, String to)
throws IOException {
InputStream in = new FileInputStream(from);
OutputStream out = new FileOutputStream(to);
try {
byte[] bytes = new byte[128 * 1024];
int len;
while ((len = in.read(bytes)) > 0) {
out.write(bytes, 0, len);
}
} finally {
if (in != null)
in.close();
if (out != null)
out.close();
}
}
}

经测试最后一种方式,速度最快!不知道是否有比他更快的方式!?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值