黑马程序员技术交流社区
标题:
代码怎么就没保存呢~~~网速的问题?
[打印本页]
作者:
㏒假™面具
时间:
2015-2-7 02:33
标题:
代码怎么就没保存呢~~~网速的问题?
~~~~~~~~~~~~
package mybufferedreader;
import java.io.FileReader;
import java.io.IOException;
/**
* @author 面具
*
*/
public class MyBufferedReader {
// 创建常量 ,定义数组长度
private static final int BUF_SIZE = 1024;
/**
* @param args
*/
private FileReader fr;
// 定义数组作为缓冲区
private char[] buf = new char[BUF_SIZE];
// 定义变量记录角标值,用于操作数组,当操作到最后一个元素是,角标归零
private int pos = 0;
// 定义计数器,记录数组存储的数据的个数,当该数据自减到0时,读取数据存入数组
private int count = 0;
public MyBufferedReader(FileReader fr) {
super();
this.fr = fr;
}
/**
* 该方法一次读取一个字符
* @return
* @throws IOException
* @see java.io.InputStreamReader#read()
*/
public int myread() throws IOException {
if(count == 0 ){
count = fr.read(buf);
pos = 0 ;
}
if(count<0)
return -1;
char ch = buf[pos];
pos++;
count--;
return ch;
/*// 1.从源文件中获取一批数据,并进行判断,只有在计数器为0时,才可以获取数据
if(count==0){
count = fr.read(buf);
if(count<0)
return -1;
// 每次获取数据后角标归零
pos = 0;
char ch = buf[pos];
pos++;
count--;
return ch;
}else if(count >0 ){
char ch = buf[pos];
pos++;
count--;
return ch;
}*/
}
public String myReadLine() throws IOException{
StringBuilder sb = new StringBuilder();
int ch;
while((ch = myread())!=-1){
// 判断字符是否为回车字符
if(ch == '\r')
continue;
if(ch == '\n')
return sb.toString();
// 将读取到的数据添加到字符串缓冲区
sb.append(ch);
}
// 最后一次读取后,判断字符串缓冲区的长度是否为0,如不为0 则将字符串缓冲区中的元素转换为字符串返回。
if(sb.length()!=0){
sb.toString();
}
return null;
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2