public class InputStreamTest {
public static void main(String[] args) {
InputStreamTest inputstream = new InputStreamTest();
FileInputStreamExample example = inputstream.new FileInputStreamExample("D:\\istream\\rechargeLarge.txt", "D:\\istream\\rechargeCopy.txt");
long start = System.currentTimeMillis();
// example.copy();
example.copyReader();
// example.copyNio();
System.out.println(System.currentTimeMillis() - start);
}
public class FileInputStreamExample {
String inputPath;
String outputPath;
public FileInputStreamExample(String inputPath, String outputPath) {
this.inputPath = inputPath;
this.outputPath = outputPath;
}
public FileInputStreamExample(String inputPath) {
this(inputPath, null);
}
public void copy() {
FileInputStream inputStream = null;
BufferedInputStream bufferInputStream = null;
FileOutputStream outputStream = null;
BufferedOutputStream bufferOutputStream = null;
try {
inputStream = new FileInputStream(inputPath);
bufferInputStream = new BufferedInputStream(inputStream);
outputStream = new FileOutputStream(outputPath);
bufferOutputStream = new BufferedOutputStream(outputStream);
int n = 0;
byte[] but = new byte[1024];
while((n = bufferInputStream.read(but)) != -1) {
//这里注意读到多少写多少
bufferOutputStream.write(but, 0, n);
}
bufferOutputStream.flush();
// System.out.println("再一次读" + inputStream.read());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferInputStream.close();
inputStream.close();
bufferOutputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void copyReader() {
FileReader fileReader = null;
BufferedReader bufferedReader = null;
FileWriter fileWriter = null;
BufferedWriter bufferedWriter = null;
try {
fileReader = new FileReader(inputPath);
bufferedReader = new BufferedReader(fileReader);
fileWriter = new FileWriter(outputPath);
bufferedWriter = new BufferedWriter(fileWriter);
int n = 0;
char[] buf = new char[1024];
while ((n = bufferedReader.read(buf)) != -1) {
bufferedWriter.write(buf, 0, n);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
fileReader.close();
bufferedWriter.close();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void copyNio() {
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
inputStream = new FileInputStream(inputPath);
outputStream = new FileOutputStream(outputPath);
inputChannel = inputStream.getChannel();
outputChannel = outputStream.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while(inputChannel.read(buffer) != -1) {
buffer.flip();
outputChannel.write(buffer);
buffer.clear();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
outputStream.close();
inputChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
在windows7平台下,做了三种IO例子的简单性能比较。通过对比发现,对于文件IO,NIO并不比流快,速度受缓冲区大小的影响,在同样缓冲区大小的条件下,按字节读写流的速度反而比NIO的速度更快。对于字符流,由于其构建在字节流的基础上,需要处理编码信息,其读写速度要比字节流慢。