packagecom.eric.io;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.ByteArrayInputStream;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjava.io.InputStream;importjava.nio.ByteBuffer;importjava.nio.CharBuffer;importjava.nio.channels.FileChannel;importcom.eric.reflect.ExecuteTimerHandler;public class ReadFileTools implementsIReadFileTools {/***
* execute readByBufferReader spend 444 million sencond!
execute readByBufferedInputStreamNoArray spend 27903 million sencond!
execute readByBufferedInputStream spend 192 million sencond!
execute readByChannel spend 484 million sencond!
execute readByChannelMap spend 42 million sencond!
execute readByDataInputStream spend 440 million sencond!
*
*@paramargs
*@throwsException*/
public static final int BUFFSIZE = 180;public static final String root = "E:\\sourcecode\\corejava\\src\\com\\eric\\io\\";public static final boolean printContext = false;public static void main(String[] args) throwsException {
String file= root + "VISA_INPUT_FULL";
IReadFileTools bi= (IReadFileTools) ExecuteTimerHandler.newInstance(newReadFileTools());
bi.readByBufferReader(file);
bi.readByBufferedInputStreamNoArray(file);
bi.readByBufferedInputStream(file);
bi.readByChannel(file);
bi.readByChannelMap(file);
bi.readByDataInputStream(file);
}/** execute readBuffer spend 421 million sencond! execute readByte spend
* 36172 million sencond!*/
publicString readByBufferReader(String file) {
StringBuilder sb= newStringBuilder();try{
BufferedReader br= new BufferedReader(new FileReader(newFile(file)));
String line;long count = 0;while ((line = br.readLine()) != null) {if(printContext) {
System.out.println(line);
}
sb.append(line);
count+=line.length();
}
br.close();
}catch(Exception ex) {
ex.printStackTrace();
}returnsb.toString();
}public void readByDataInputStream(String file) throwsException {
DataInputStream dis= new DataInputStream(new ByteArrayInputStream(newReadFileTools().readByBufferReader(file).getBytes()));while (dis.available() > 0) {char c = (char) dis.read();if(printContext) {
System.out.println(c);
}
}
}//this method not use byte array to get byte
publicString readByBufferedInputStreamNoArray(String file) {try{
InputStream is= new BufferedInputStream(new FileInputStream(newFile(file)));while (is.available() > 0) {char c = (char) is.read();if(printContext) {
System.out.println(c);
}
}
}catch(Exception ex) {
ex.printStackTrace();
}return null;
}//use byte array to get bytes from file
public void readByBufferedInputStream(String file) throwsException {
BufferedInputStream input= new BufferedInputStream(newFileInputStream(file));byte[] bytes = new byte[BUFFSIZE];while (input.available() > 0) {
input.read(bytes);
}
}//use file channel to get byte from file
public void readByChannel(String file) throwsException {
FileChannel in= newFileInputStream(file).getChannel();
ByteBuffer buffer=ByteBuffer.allocate(BUFFSIZE);while (in.read(buffer) != -1) {
buffer.flip();//Prepare for writing
if(printContext) {
System.out.println(buffer.getChar());
}
buffer.clear();//Prepare for reading
}
in.close();
}//use MappedByteBuffer to read byte from file
public void readByChannelMap(String file) throwsException {
FileChannel fc= new FileInputStream(newFile(file)).getChannel();
CharBuffer cb= fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).asCharBuffer();charc;while(cb.hasRemaining())
c=cb.get();if(printContext) {
System.out.println(c);
}
fc.close();
}public void copyFileByChannel(String file, String file2) throwsException {
FileChannel in= newFileInputStream(file).getChannel();
FileChannel out= newFileOutputStream(file2).getChannel();
ByteBuffer buffer=ByteBuffer.allocate(BUFFSIZE);while (in.read(buffer) != -1) {
buffer.flip();//Prepare for writing
out.write(buffer);
buffer.clear();//Prepare for reading
}
}public voidtest() {
System.out.println("test");
}public void copyFile(String source, String dest) throwsException {
BufferedReader br= new BufferedReader(new FileReader(newFile(source)));
BufferedWriter bw= new BufferedWriter(new FileWriter(newFile(dest)));
String temp;while ((temp = br.readLine()) != null) {
bw.write(temp+ "\n");
}
}public void storingAndRecoveringData(String file) throwsException {
DataInputStream dis= new DataInputStream(new BufferedInputStream(newFileInputStream(file)));
DataOutputStream dos= new DataOutputStream(new BufferedOutputStream(newFileOutputStream(file)));
dos.writeBoolean(false);
dos.writeByte(10);
dos.writeDouble(1213654);
dos.writeUTF("aihua");
dos.close();
System.out.println(dis.readBoolean());
System.out.println(dis.readByte());
System.out.println(dis.readDouble());
System.out.println(dis.readUTF());
dis.close();
}public void doCopyFile(String src, String dest) throwsIOException {
File srcFile= newFile(src);
File destFile= newFile(dest);if(destFile.exists()) {boolean d =destFile.delete();if(d) {
System.out.print("删除成功!");
}else{
System.out.print("删除失败!");
}
}
BufferedInputStream input= new BufferedInputStream(newFileInputStream(srcFile));try{
BufferedOutputStream output= new BufferedOutputStream(newFileOutputStream(destFile));try{byte[] buffer = new byte [4096];int n = 0;while (-1 != (n =input.read(buffer))) {
output.write(buffer,0, n);
}
System.out.println("Copy Successful::" +dest);
}finally{try{if (output != null) {
output.close();
}
}catch(IOException ioe) {
ioe.printStackTrace();
}
}
}finally{try{if (input != null) {
input.close();
}
}catch(IOException ioe) {
System.out.println("failed src file:" + src + " reason:" +ioe.getMessage());
}
}
}
}/**
* History:
*
*
*
* $Log: $*/