Buffer简析
写模式
读模式
都读完:
读完了,想变成写模式,调用clear()函数
position到最开始的位置,limit到capacity位置
只读取一部分数据:
但是我又想回到写模式,这时候就调用compact()函数
Channel简析
代码演示几种拷贝文件的方法
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
interface FileCopyRunner{
void copyFile(File source, File target);
}
public class FileCopyDemo {
private static final int ROUNDS = 5;
private static void benchmark(FileCopyRunner test,File source, File target){
long elapsed = 0L;
for (int i =0; i < ROUNDS; i++){
long startTime = System.currentTimeMillis();
test.copyFile(source,target);
elapsed += System.currentTimeMillis() - startTime;
target.delete();
}
System.out.println(test + ": " + elapsed / ROUNDS);
}
private static void close(Closeable closeable){
if (closeable != null){
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
FileCopyRunner noBufferStreamCopy = new FileCopyRunner() {
@Override
public void copyFile(File source, File target) {
InputStream fin = null;
OutputStream fout = null;
try {
fin = new FileInputStream(source);
fout = new FileOutputStream(target);
int result;
while ((result = fin.read()) != -1){
fout.write(result);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
close(fin);
close(fout);
}
}
@Override
public String toString() {
return "noBufferStreamCopy";
}
};
FileCopyRunner bufferedStreamCopy = new FileCopyRunner() {
@Override
public void copyFile(File source, File target) {
InputStream fin = null;
OutputStream fout = null;
try {
fin = new BufferedInputStream(new FileInputStream(source));
fout = new BufferedOutputStream(new FileOutputStream(target));
byte[] buffer = new byte[1024];
int result;
while ((result = fin.read(buffer)) != -1){
fout.write(buffer,0, result);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
close(fin);
close(fout);
}
}
@Override
public String toString() {
return "bufferedStreamCopy";
}
};
FileCopyRunner nioBufferCopy = new FileCopyRunner() {
@Override
public void copyFile(File source, File target) {
FileChannel fin = null;
FileChannel fout = null;
try {
fin = new FileInputStream(source).getChannel();
fout = new FileOutputStream(target).getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while ((fin.read(buffer)) != -1){
buffer.flip();
while (buffer.hasRemaining()){
fout.write(buffer);
}
buffer.clear();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public String toString() {
return "nioBufferCopy";
}
};
FileCopyRunner nioTransferCopy = new FileCopyRunner() {
@Override
public void copyFile(File source, File target) {
FileChannel fin = null;
FileChannel fout = null;
try {
fin = new FileInputStream(source).getChannel();
fout = new FileOutputStream(target).getChannel();
long transferred = 0L;
long size = fin.size();
while (transferred != fin.size()){
transferred += fin.transferTo(0, size, fout);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
close(fin);
close(fout);
}
}
@Override
public String toString() {
return "nioTransferCopy";
}
};
}
}