展开全部
如果只e69da5e887aa62616964757a686964616f31333335343333是你的文件只是存储字符的话,可以用缓冲字符流来读取,这样更高效。import java.io.IOException;
import java.io.Reader;
/*
* 模拟一个缓冲区
* 基于已有的缓冲区思想,我们可以从源读取用read方法。
* 我们的缓冲区,应该是一个更高效的read读取方法。
*/
public class MyBufferTest extends Reader{
privateReader r;
privatechar[] buf = new char[1024];
privateint count = 0,pos = 0;
publicMyBufferTest(Reader r){
this.r = r;
}
/**
* 一次从缓冲区中取一个
* @return 返回一个缓冲区中的字符
* @throws IOException
*/
publicint myRead() throws IOException {
//1,首先判断缓冲区中是否有数据,如果没有就从源中去拿。
if(count == 0){
count= r.read(buf);
pos= 0;
}
//2,当缓冲区中没数据了且源中也没有数据时,count自减1小于0时就返回-1结束.
if(count
return-1;
//3,如果以上都不满足,那么从缓冲区中写入一个字符到新的文件中。
char ch = buf[pos];
pos++;
count--;
return ch;
}
/**
* 按照文本特点,提供一个特有的操作文本的方法。
* 一次读取一行文本,只要是到行结束符之前的文本即可。
* @return 返回读取到的一行文本
* @throws IOException
* 原理:
* 就是从缓冲区中取出数据,并存储到一个临时容器中。
* 如果取到了回车符,就将临时容器中的数据转成字符串返回。
*/
publicString myReadLine() throws IOException{
//1,定义一个临时容器,进行临时存储
StringBuilder sb = new StringBuilder();
//2,定义一个变量,接收读取到的字符,也就是转成ask码表后的一个int型数字
int ch = 0;
while((ch = myRead()) != -1){
//3,当读取到\r时,直接跳出本次循环
if(ch== '\r')
continue;
//4,当读取到\n时,直接跳出当前循环
if(ch== '\n')
return sb.toString();
//5,当都没有读取到时,就将这些数据存储到临时容器中。
sb.append((char)ch);
}
//6,当临时容器中的长度不等于0时,就输出字符。
if(sb.length() != 0)
returnsb.toString();
return null;
}
@Override
publicvoid close() throws IOException {
}
@Override
publicint read(char[] arg0, int arg1, int arg2) throws IOException {
return 0;
}
}