A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© trhthyj 中级黑马   /  2014-4-7 21:41  /  1067 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

ch不是int行的吗?怎么会ch=='\r'呢?

评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 神马都是浮云

查看全部评分

3 个回复

倒序浏览
  1. import java.io.*;
  2. class Demo1
  3. {
  4.         private FileReader fr;
  5.         Demo1(FileReader fr)
  6.         {
  7.                 this.fr = fr;
  8.         }
  9.         public  String show()throws IOException
  10.         {        StringBuilder sb = new StringBuilder();
  11.                 int len = 0;
  12.                 while((len= fr.read())!=-1)
  13.                 {
  14.                         if(len=='\r')//字符在马表里面对应的都是,数字。
  15.                                 continue;
  16.                         if(len=='\n')
  17.                                 return sb.toString();
  18.                         else
  19.                                 sb.append((char)len);
  20.                 }
  21.                 if((sb.length())!=0)
  22.                         return sb.toString();
  23.                 return null;
  24.         }
  25.         public  void myClose()throws IOException
  26.         {
  27.                 fr.close();
  28.         }
  29.         public void setLine(int len)
  30.         {
  31.                 this.len= len;
  32.         }
  33.         public int getLen()
  34.         {
  35.                 return len;
  36.         }
  37. }
  38. class Demo
  39. {
  40.         public static void main(String[] args)throws IOException
  41.         {
  42.                 FileReader fr = new FileReader("Demo.java");
  43.                 Demo1 d= new Demo1(fr);
  44.                 String leng= null;
  45.                 while((leng=d.show())!=null)
  46.                 {
  47.                         System.out.println(leng);
  48.                 }
  49.                 d.myClose();
  50.         }
  51. }
复制代码
回复 使用道具 举报
返回的就是int类型啊,API这么写的;
read
public int read()
         throws IOException读取单个字符。在字符可用、发生 I/O 错误或者已到达流的末尾前,此方法一直阻塞。
用于支持高效的单字符输入的子类应重写此方法。

返回:
作为整数读取的字符,范围在 0 到 65535 之间 (0x00-0xffff),如果已到达流的末尾,则返回 -1
抛出:
IOException - 如果发生 I/O 错误

评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 赞一个!

查看全部评分

回复 使用道具 举报
  1. import java.io.*;
  2. class MyBufferedInputStream {
  3.         //定义一个要被修饰的类
  4.         private InputStream in;
  5.         //定义一个字节型的数组
  6.         private byte[] buf=new byte[1024];
  7.         //定义两个变量,一个做指针,一个做计数器
  8.         private int pos=0,count=0;
  9.         //构造函数,向构造函数里面添加被修饰的类
  10.         public MyBufferedInputStream(InputStream in){
  11.                 this.in=in;
  12.         }
  13.         //自定义一个read方法
  14.         public int myRead()throws IOException{
  15.                 //计数器大于0时,
  16.                 if (count>0) {
  17.                         byte b=buf[pos];
  18.                         count--;
  19.                         pos++;
  20.                         return b&15;
  21.                 }else if (count==0) {//计数器为零时,说明数组中的元素被取完了,需要重新获取
  22.                         count=in.read(buf);
  23.                         //数组获取不到元素,说明已经读取完毕
  24.                         if (count<0) {
  25.                                 return -1;
  26.                         }
  27.                         //将指针置为数组的0角标
  28.                         pos=0;
  29.                         byte b=buf[pos];
  30.                         count--;
  31.                         pos++;
  32.                         return b&15;
  33.                 }
  34.                 return -1;
  35.         }
  36.         public void myClose()throws IOException{
  37.                 in.close();
  38.         }
  39.        
  40. }
复制代码

read方法都到最后为了防止第一次读到的就是-1,会把读到的字节&15提升到int类型,所以返回来的是int型数据

评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马