io,nio和操作系統命令性能比較

package org.clarance;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class TestNIO {
public void t1(String p1, String p2) {
long b = System.currentTimeMillis();
String cmd = "cmd /c copy " + p1 + " " + p2;
java.lang.Process p = null;
try {
p = java.lang.Runtime.getRuntime().exec(cmd);
p.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
finally {
if(p != null) p.destroy();
}
System.out.println("t1 cost :: " + (System.currentTimeMillis() -b));
}

public void t2(String p1, String p2) {
long b = System.currentTimeMillis();
InputStream in = null;
OutputStream out = null;
try {
in = new java.io.FileInputStream(p1);
out = new java.io.FileOutputStream(p2);
byte[] data = new byte[1024];
int n = 0;
while((n = in.read(data)) != -1) {
out.write(data, 0, n);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
if(in != null) {
in.close();
}
if(out != null) {
out.close();
}
}
catch(IOException ee) {

}
}
System.out.println("t2 cost :: " + (System.currentTimeMillis() -b));

}

public void t3(String p1, String p2) {
long b = System.currentTimeMillis();
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(p1);
out = new FileOutputStream(p2);
FileChannel fc = in.getChannel();
FileChannel fc2 = out.getChannel();
ByteBuffer bf = ByteBuffer.allocateDirect(1024);
while(fc.read(bf) != -1) {
bf.flip();
fc2.write(bf);
bf.clear();
}
fc.close();
fc2.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}

System.out.println("t3 cost :: " + (System.currentTimeMillis() -b));
}

public void t4(String p1, String p2) {
long b = System.currentTimeMillis();
java.io.RandomAccessFile rf = null;
java.io.RandomAccessFile rf2 = null;

try {
rf = new java.io.RandomAccessFile(p1, "rw");
rf2 = new java.io.RandomAccessFile(p2, "rw");
FileChannel fc1 = rf.getChannel();
FileChannel fc2 = rf2.getChannel();
fc1.transferTo(0, fc1.size(), fc2);

fc1.close();
fc2.close();

}
catch(Exception e) {

}
finally {

try {
if(rf != null) rf.close();
if(rf2 != null) rf2.close();
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
}

System.out.println("t4 cost :: " + (System.currentTimeMillis() -b));
}

public static void main(String[] args) {
TestNIO tn = new TestNIO();
final String p1 = "F:\\downloads\\platform921_win32.exe";
final String p2 = "E:\\downloads\\";
final String n1 = "p10.exe";
tn.t1(p1, p2+1+n1);
tn.t2(p1, p2+2+n1);
tn.t3(p1, p2+3+n1);
tn.t4(p1, p2+4+n1);

}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章